How to print PDF directly to the printer
We use following approach to build print view of documents in our web-based application. At the begging the document template is filled by real data and HTML page is generated. After that this page is sent to HTMLDOC which converts it into PDF. The users open the document via Acrobat plugin installed in the web browser. This approach has worked fine for years.
But recently we decided to send PDF directly to the printer. I’ve added printer to the our server and implemented a simple function (we have a Xerox printer with PostScript support):
lp -d printer_name -o sides=two-sided-long-edge -o media=A4 -o portrait -o page-ranges=1-7 PDF_file
The strange thing appeared right after that. One type of documents couldn’t print anymore. No errors in the CUPS log or on the printer display. The job just disappeared from the queue and was marked as printed. After spending some time I found the way to solve this problem. I use acroread to convert PDF into PostScript and then send it to the printer:
acroread -toPostScript PDF_file
Acroread creates the PostScript file with the same name but with different extension (.ps) and stores it in the current directory. But you can pass desired directory name as second parameter:
acroread -toPostScript PDF_file /store-dir/
I use Acrobat 5 from DAG repository. It works fine even the server doesn’t have installed X. But if you want to use Acrobat 7 you should do additional work. It needs X and if try to run it from command line it give you an error:
(acroread:6488): GTK-WARNING **: cannot open display:
To solve this just install Xvfb – X server that can run on machines with no display hardware and no physical input devices, and run following command:
xinit acroread -toPostScript PDF_file /store-dir/ -- /usr/X11R6/bin/Xvfb :9 -ac