Friday, January 29, 2010

Print to PDF from the Linux Terminal

A while back I had to set up a system for printing reports to PDF files automatically. IE: I needed a script to do the conversion, retrieve the new file, fix the orientation, and return the new filename. Here is the script and the details and the requirements.

  • CUPS-PDF: This package provides a PDF printer that we can print to using lp.
  • TexLive: This is a large project with many tools and has a large footprint but it is necessary for possessing landscape jobs. The exact problem is that jobs printed in landscape will be miss oriented when viewed and this allows us to correct that problem.
  • pdfjam: The project listed but does not need to be installed, rather a custom version of the pdf90 script included in this project is needed. Specifically the script is customized to rotate the page counter-clockwise.
  • printpdf: This is the main script and can be called from any external tools.
  • pdf90minus: This is the modified version of pdf90 from pdfjam that rotates the pages of a PDF counter-clockwise.
  • texlive.profile: This is a installer configuration for TexLive with just the necessary components selected.

Install CUPS-PDF and configure a printer for it. To configure the printer you can use the CUPS web interface or add the following lines manually.
<printer pdf="">
Info PDF Writer for CUPS
Location PDF Backend /usr/lib64/cups/backend/pdf-writer
DeviceURI pdf-writer:/tmp/
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0

Install TexLive. To install the minimal required components should download the installer and the profile and extract them to the same location. then run the following command from that folder.
sudo ./install-tl −profile texlive.profile

Save the pdf90minus and pdfprint scripts to the same location. You may wish to customize the working path /tmp but it must be the same in the printer configuration and printpdf script.


You can now test the PDF printing, the job will print and the PDF will be generated at the same path with the same name as teh original file only suffixed with .pdf.
./printpdf myfile.txt [--rotate|-r] [--save|-s]

The --rotate option causes the job to be printed in landscape mode and then the PDF pages to be reoriented to display properly.
The --save option causes the source file to be left behind and not deleted once successful printed.
The script will block execution until the print job has finished and will then return passing the new filename to STDOUT.

1 comment:

lauren said...

I was always doing the task of printing the PDF files manually by first saving it to some folder and then opening and printing it.Its acceptable unless the reports are less but when number increase then it need to be automated to save effort and time
electronic signatures