Print a specific file as a PDF
Power Automate Desktop has a built-in action that prints a file by its path, but you can’t choose which printer to use.
Because the file is printed on the default printer, you may not be able to print it as a PDF.
This article explains how to print a specific file as a PDF.
Solution
Section titled “Solution”Power Automate Desktop provides an action that changes the default printer and an action that gets the name of the current default printer.
With these, you can output a PDF by saving the name of the default printer in a variable before printing, changing the default printer to a PDF printer, and then restoring the original default printer after printing.
Implement by copying and pasting
Section titled “Implement by copying and pasting”As a sample, here’s a scenario that prints a text file named “document.txt”, saved in the Public Documents folder, as a PDF.
Copy the following code and paste it into the Power Automate Desktop flow designer to add the actions.
Workstation.GetDefaultPrinter PrinterName=> PrinterNameWorkstation.SetDefaultPrinter PrinterName: $'''Microsoft Print to PDF'''Workstation.PrintDocument DocumentPath: $'''C:\\Users\\Public\\Documents\\document.txt'''WAIT 1Workstation.SetDefaultPrinter PrinterName: PrinterNameImplement in Power Automate Desktop
Section titled “Implement in Power Automate Desktop”- In “Search actions”, type “Get default printer” and add the action. The name of the current default printer is saved in the variable
PrinterName. - In “Search actions”, type “Set default printer” and set a PDF printer in “Printer name” (for example, “Microsoft Print to PDF”, which comes with Windows, or “Adobe PDF” if you have Adobe Acrobat installed).
- In “Search actions”, type “Print document” and specify the path of the file you want to print.
- Again, in “Search actions”, type “Set default printer” and set “Printer name” to the variable
%PrinterName%that you saved in step 1.
Handling errors
Section titled “Handling errors”If the scenario ends with an error partway through, the default printer may be left changed. We recommend adding an “On block error” block so that the default printer is always restored, even when an error occurs.