biopdf logo

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.

bioPDF Command Line Print

USAGE:
  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.

@ECHO OFF

@REM Set environment variables used by the batch file
SET PRINTERNAME=PDF Writer - bioPDF

REM Create runonce.ini
SET LAPP=%LOCALAPPDATA%
IF "%LAPP%"=="" SET LAPP=%USERPROFILE%\Local Settings\Application Data
SET RUNONCE=%LAPP%\PDF WRITER\%PRINTERNAME%\runonce.ini
ECHO %RUNONCE%
IF EXIST "%RUNONCE%" DEL "%RUNONCE%"

ECHO Save settings to "%RUNONCE%"
ECHO [PDF Printer] >>  "%RUNONCE%"
ECHO output=%CD%\out\demo.pdf >> "%RUNONCE%"
ECHO author=Demo Script >> "%RUNONCE%"
ECHO showsettings=never >> "%RUNONCE%"
ECHO showpdf=no >> "%RUNONCE%"
ECHO watermarktext=Batch Demo >>  "%RUNONCE%"
ECHO confirmoverwrite=no >>  "%RUNONCE%"
printto.exe "in\example.rtf" "%PRINTERNAME%"
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. The files needed are available here.

Example files (zip archive)

 
McAfee SiteAdvisor Norton Safe Web