True PDF Software
for you!

  • Deutsch
  • English

Command Line Printing to PDF

Have you ever wanted to automate printing using a command line or a batch file? Then this example might be something for you. It will show you how to control the PDF Writer from a batch file. This example will enable you to print a wide range of document types to PDF from the command line.

Using Microsoft Windows you can right click your documents and select print from the context menu. This behavior is supported by most of the common document types. Each document type has an associated program that will take care of the printing. These associations are set up in the computer's registry database. With this example you will be able to automatically print most of the documents, which support with the "print" option in the context menu.

In order to run this example a small command line utility is needed. It is the printto.exe program which will ask the document to print itself to any given printer. Please note that this command line printing tool requires .Net Framework 2.0.

  1. 7-PDF Command Line Print
  2.  
  3. USAGE:
  4.    printto "file name" ["printer name"]

With this tool on the system you can automate printing from the command line as shown in the example batch file below.

  1. @ECHO OFF
  2.  
  3. @REM Set environment variables used by the batch file
  4. SET PRINTERNAME=7-PDF Printer
  5.  
  6. REM Create runonce.ini
  7. SET LAPP=%LOCALAPPDATA%
  8. IF "%LAPP%"=="" SET LAPP=%USERPROFILE%\Lokale Einstellungen\Anwendungsdaten
  9. SET RUNONCE=%LAPP%\PDF WRITER\%PRINTERNAME%\runonce.ini
  10. ECHO %RUNONCE%
  11. IF EXIST "%RUNONCE%" DEL "%RUNONCE%"
  12.  
  13. ECHO Save settings to "%RUNONCE%"
  14. ECHO [PDF Printer] >>  "%RUNONCE%"
  15. ECHO output=%CD%\out\demo.pdf >> "%RUNONCE%"
  16. ECHO author=Demo Script >> "%RUNONCE%"
  17. ECHO showsettings=never >> "%RUNONCE%"
  18. ECHO showpdf=no >> "%RUNONCE%"
  19. ECHO watermarktext=Batch Demo >>  "%RUNONCE%"
  20. ECHO confirmoverwrite=no >>  "%RUNONCE%"
  21. printto.exe "in\example.rtf" "%PRINTERNAME%"
  22. ECHO ERRORLEVEL=%ERRORLEVEL%

The code will print the example.rtf file to the PDF Writer and save the result as demo.pdf.

Download Example Files

You can download and run the example yourself.

AttachmentSize
Package icon Example files13.32 KB