PDFtoPrinter: Command-line PDF printing


A utility that prints PDF files from the Windows command line

PDFtoPrinter is a program for printing PDF files from the Windows command line. You may download it here. The program is designed generally for the Windows command line and also for use with the vDos DOS emulator (see below).

New version, 19 October 2024: A new version of the app should avoid problems with qpdf29.dll that may have occurred with earlier versions.

Note: In some network setups, when printing from an Apache server, the current version may not print correctly. This older version should work correctly if the current one does not. (You might also try this .Net wrapper for the application; I have not tried it.)

Malware false positives. Some badly-written anti-malware programs will report that my installer contains malware; this happens because the AutoIt scripting language has been used to create malware and anti-malware programs block anything created in AutoIt. If you don't trust my software, don't use my software! Find something else instead! (Or compile it yourself from the code on the GitHub repository for the software.)


You may not need this program at all

This program seems to be the only free standalone program that prints PDF files from the Windows command line without unwanted side effects. (Sumatra PDF can print from the Windows command line, but cannot choose the paper source in the printer according to the PDF size, so all PDFs print on the same paper size.)

However, Windows Powershell can also print PDF files from the command line, and if Powershell can do what you need, then you don't need the PDFtoPrinter program. This method only works if your default PDF viewer supports it; Adobe Acrobat does not support this method.

For example, to print to a specific printer, use this Powershell command:

Start-Process -FilePath "path\to\file" -Verb PrintTo -ArgumentList "Name of Printer" -PassThru | %{sleep 10;$_} | kill

Or to print to the default printer, use this command:

Start-Process -FilePath "path\to\file" -Verb Print -PassThru | %{sleep 10;$_} | kill

Also see the paragraph below about alternatives to the PDFtoPrinter app, which explains how to use the PDF-Xchange Viewer app embedded in the PDFtoPrinter app, but without the PDFtoPrinter "wrapper."


The program and how it works

The program is a compiled AutoIt script that effectively serves as a "wrapper" around the free PDF-Xchange Viewer, which is included in the PDFtoPrinter program itself. (Someone asked whether I wrote the script; yes, I did; but it has been improved by a programmer who prefers to remain anonymous.) PDF-Xchange Viewer is the only no-cost PDF software that I know of that does all of the following:

You can use PDF-Xchange Viewer (or its successor, PDF-XChange Editor) to do everything that the PDFtoPrinter program can do, but PDFtoPrinter is easier to use for its single purpose of printing from the command line. Also, PDFtoPrinter requires no setup and always maintains the correct settings for its single purpose. PDF-Xchange Viewer, when used as a standalone program, is infinitely more flexible, but at the cost of readily losing the settings required for "Select paper source from page size."

Technical questions: If you have technical questions about printing from this application (e.g., which PDF version is required by the software, if any), don't waste your valuable time asking me, because I don't know the answer. Remember: PDFtoPrinter.exe is a wrapper around PDF-Xchange Viewer, by Tracker Software. Only Tracker Software will know the answers to your questions, and you should of course buy a license for their software before you ask them for help.

A programmer who wishes to remain anonymous provided major enhancements to this software in August 2021. These include the ability to print multiple files, recurse through subdirectories, detect encrypted PDF files, generate a CSV file that lists files printed, reverse-page-number selection, and more.

To specify a non-default temporary folder for the app's working files, add the command-line switch:

PDFtoPrinter /mydir=c:\path\to\folder

If the path contains spaces, use quotation marks around the path. The app will try to create the folder if it does not exist. By default, if this switch is not used, the app creates a folder in the user's temporary folder and copies its files into that folder; if Windows blocks the folder from being created, use this command-line switch to access or create a folder to which you have access.

To print a PDF file to the default Windows printer, use this command:

PDFtoPrinter filename.pdf

You can use a full path for the filename, but if the path or filename contains spaces, use quotation marks around the path and filename.

Alternatively, you can simply drop a PDF file on to the application (or on a shortcut to it).

To print to a specific printer, add the name of the printer in quotation marks:

PDFtoPrinter filename.pdf "Name of Printer"

If you want to print to a network printer, use the name that appears in Windows print dialogs, like this (and be careful to note the two backslashes at the start of the name and the single backslash after the servername):

PDFtoPrinter filename.pdf "\\SERVER\PrinterName"

If you mistype the printer name or specify a printer that does not exist, nothing will print, but no error message will be displayed.

To select a printer from a list of installed printers, change the name of the program to PDFtoPrinterSelect.exe. When launched, it will pop up a list of installed printers, unless you specify a printer name on the command-line, in which case it will print to the specified printer, without displaying a menu.

To specify a page range: Whether or not you specify a printer, you can specify a page range to print by adding an optional parameter, like this (no spaces around the equals sign):

PDFtoPrinter filename.pdf pages=#-#

Separate multiple page ranges with commas (no spaces) like this: 2-4,7,12 or, to specify all pages after a specific page, use its number followed by a hyphen, like this: 7- . You can also use z-1 for reverse printing of the last pages in the number, or z:-1:odd|even to reverse print odd or even pages, or r5-r2 for fifth-to-last to second-to last pages.

To print multiple copies: To print more than one copy, add an optional parameter, like this (no spaces around the equals sign):

PDFtoPrinter filename.pdf copies=#

To print a password-protected PDF: Specify /p:password on the command line.

To restore Windows focus to a calling program: If you launch PDFtoPrinter.exe from another program, this optional parameter tries to restore the Windows focus to the window with the title specified in the parameter:

PDFtoPrinter filename.pdf focus="My Calling Program"

You may omit the quotation marks if the window title of the calling program has no spaces.

To run silently, with no user feedback or focus change: Specify /s on the command line.

To print multiple PDF files: Specify a wildcard (e.g. A*.pdf) on the command line; this command, by default recursively searches subdirectories. You can also use the /r switch when specifying a single filename, or /Rn to recurse through n depth of subdirectories.

To generate a CSV file listing multiple files printed: Specify /csv on the command line; CSV will be written to %temp%\pdftoprintertemp. Specify /mock to generate the CSV only, but not print any files.

Optional parameters may be combined.

Customization: By default, this system uses these printing options as set in PDF-XChange Viewer: Scaling is set to None; Auto-rotate Sheets; Auto-centre pages in sheets; and Choose paper source by PDF-page size. If you want to change any of these, or choose any other options, download and install PDF-XChange Viewer; open a PDF file, go to the Print menu, set your desired options (and, just to be certain, print the file). Then go to the Edit menu and choose Export All Settings to Data File... Accept the suggested filename, "PDF-XChange Viewer Settings.dat", and save the file to a convenient location. Move or copy the file to the same folder with PDFtoPrinter.exe. When you next run PDFtoPrinter.exe, it will use the settings saved in that file.

You may find the PDF-XChange viewer manual at this link.

Duplex printing: PDF-XChange Viewer cannot save a duplex-printing option as the default. To print in duplex mode, modify the settings of your Windows printer (in the Windows Settings or Control Panel app) to enable duplex printing, or install your printer a second time, using a different name, and set it to print duplex by default; then specify that duplex-default printer when running PDFtoPrinter.exe. Search the web if you don't know how to install your printer a second time, or if you don't know how to specify duplex printing in your Windows printer properties.

Troubleshooting:

If you run this program from a batch or Windows .CMD file, and the program doesn't print your document, or if the the PDF-XChange Viewer opens when you run PDFtoPrinter.exe, then edit your batch file so that it sets the folder that contains the program as the current working directory.

If in any situtation, if the program doesn't print your document, or if the PDF-XChange Viewer opens when you run PDFtoPrinter.exe, you can help to identify the problem and possibly solve it. Either rename PDFtoPrinter.exe to a name that includes "debug", perhaps PDFtoPrinter-debug.exe (or make a copy of the program and rename the copy so that its name includes "debug"). Alternatively, run the program (with its original name) with the command-line switch /debug (no quotation marks) added to whatever command-line switches you already use. When you run the program, using either of these methods, it will copy the PDF-Xchange.exe print command to the Windows clipboard; you can then paste this command into a Windows command prompt to see what may have gone wrong.

You may not need this program at all: First, consider the Powershell printing method described at the top of this page, which may or may not work on your system.

Alternatively, if you want to print PDF files repeatedly from the command line, for example in a production environment, consider doing this. First run PDFtoPrinter.exe from the command line until it prints your file correctly. Then run PDFtoPrinter.exe with the same command line, but also with the "debug" option described above. PDFtoPrinter will place the PDF-Xchange.exe command line on the Windows clipboard; paste it into a convenient place. Then download and install the current version of the PDF-Xchange Editor (paid commercial software) and set up your system to print from the PDF-Xchange Editor with command-line parameters like the one created by PDFtoPrinter.exe. For example, you could put that command into a Windows .CMD file or any other automated procedure. You can now print from the command-line without using PDFtoPrinter.exe to do the job for you.


Printing from vDos (and vDosPlus)

You may use this program with vDos to print PCL or PostScript output to any Windows printer (applies to vDos 2016.10.01 and later versions). Place PDFtoPrinter.exe in the folder with the vDos.exe program. If you want to print PCL output, you must add pcl6.exe to the same folder; if you want to print PostScript output, you must add gswin32c.exe to the same folder.  If you do not have these files, you may download them here (from the vDosPCLPS.zip archive linked near the top of the page). Then use this syntax in config.txt (replacing LPT1 and #LPT1.PDF with LPT2 and #LPT2.PDF, etc., if needed):

LPT1 = "PDFtoPrinter.exe" #LPT1.PDF

You can specify a printer by adding the printer name in quotation marks, like this:

LPT1 = "PDFtoPrinter.exe" #LPT1.PDF "Microsoft XPS Document Writer"

If you want to print to a network printer, use the name that appears in Windows print dialogs, like this (and be careful to note the two backslashes at the start of the name and the single backslash after the servername):

LPT1 = "PDFtoPrinter.exe" #LPT1.PDF "\\SERVER\PrinterName"

If you want to select from a list of installed printers, change the name of the program to PDFtoPrinterSelect.exe (and, of course, change the line in config.txt to refer to "PDFtoPrinterSelect.exe"). The program will pop up a list of installed printers, unless you specify a printer name in the command string, in which case it will print to the specified printer, without displaying a menu.

Use the same procedure with vDosPlus (2016.10.01 and later versions), placing the additional files in the same folder with vDosPlus.exe.


Source code

If anyone wants to see the amateurish and probably incompetent AutoIt source code used for compiling the PDFtoPrinter.exe executable, you can find it on this GitHub repository; feel free to modify it and suggest improvements. The external files required for building the executable are also available from the GitHub page. Before compiling, remove the line near the top that contains "Run_After" (this line refers to my code-signing routine).


Licensing

This system uses the free PDF-Xchange Viewer. You should consult the linked page to determine whether or not you may use that program for your own purposes. The compiled AutoIt script that wraps the PDF software is free for use by anyone, anywhere, for any purposes whatever. Do not waste your valuable time asking me about a license for the AutoIt wrapper; you don't need one.


Edward Mendelson (edward [dot] mendelson [at] columbia [dot] edu).