Tag Archives: html

How to convert a document to PDF using bash in OS X

Below we have a script called 2PDF.sh. It was motivated to automatically have our servers convert digital book format (xml or html) files to PDF’s so that the resources can be shared amongst our faculty and students, since PDF’s worked well with our existing assistive software.


# First make sure that you are passing the file as the first
# argument, and it exists.
if [ $# = 1 ] && [ -e $1 ]; then

  #Print the file as a PDF to stdout using CUPS, redirect stderr so no error output is visible.
  cupsfilter $1 2> /dev/null


This very simple script is run on one of our OS X (10.6) servers when a new digital book is uploaded. This script is run with the file passed as its first argument. Since the output is directed to stdout we can redirect it anywhere, giving us maximum flexibility.

So this script might be run as follows:

./2PDF.sh popular_mechanics.xml > popular_mechanics.pdf

This quite simply just takes the file popular_mechanics.xml file converts it to a pdf and redirects its output to a new file called popular_mechanics.pdf.

Our script is more complicated that what is presented but I just wanted to focus on how in OS X and other platforms that use CUPS can utilize bash to script PDF creation with no additional software or external scripts.

Performance Optimization WordPress Plugins by W3 EDGE