tinyapps.org / blog


OS X: Batch convert and concatenate HTML files to a single PDF file #

with this Bash one-liner:
for i in *.html; do /System/Library/Printers/Libraries/convert -m application/pdf -f "$i" -o $(basename -s ".html" "$i").pdf; done && "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o combined.pdf *.pdf
(via cybercape and Fritz)

UPDATE: /System/Library/Printers/Libraries/convert last appeared in OS X 10.7 Lion; even back then, it was a symlink to /usr/sbin/cupsfilter:

$ ls -lah /System/Library/Printers/Libraries/convert
lrwxr-xr-x  1 root  wheel    20B May 14  2012 /System/Library/Printers/Libraries/convert -> /usr/sbin/cupsfilter

$ test -L /System/Library/Printers/Libraries/convert && echo $?
0

though their options differed somewhat:

$ /System/Library/Printers/Libraries/convert
Usage: convert [ options ]
Options:
  -D                      Remove the input file when finished.
  -J title                Set title.
  -P filename.ppd         Set PPD file.
  -U username             Set username for job.
  -a 'name=value ...'     Set option(s).
  -c copies               Set number of copies.
  -d printer              Use the named printer.
  -e                      Use every filter from the PPD file.
  -f filename             Set file to be converted (otherwise stdin).
  -i mime/type            Set input MIME type (otherwise auto-typed).
  -j mime/type            Set output MIME type (otherwise application/pdf).
  -o filename             Set file to be generated (otherwise stdout).
  -u                      Remove the PPD file when finished.

$ /usr/sbin/cupsfilter
Usage: cupsfilter [ options ] filename
Options:
  -D                      Remove the input file when finished.
  -P filename.ppd         Set PPD file.
  -U username             Set username for job.
  -c cupsd.conf           Set cupsd.conf file to use.
  -d printer              Use the named printer.
  -e                      Use every filter from the PPD file.
  -i mime/type            Set input MIME type (otherwise auto-typed).
  -j job-id[,N]           Filter file N from the specified job (default is file 1).
  -m mime/type            Set output MIME type (otherwise application/pdf).
  -n copies               Set number of copies.
  -o name=value           Set option(s).
  -p filename.ppd         Set PPD file.
  -t title                Set title.
  -u                      Remove the PPD file when finished.

So a modern equivalent of the above one-liner would be:

$ for i in *.html ; do cupsfilter "$i" > "$i".pdf ; done && "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o combined.pdf *.pdf

See also the open source, cross-platform wkhtmltopdf for rendering HTML into PDF.

/mac | Sep 07, 2013


Subscribe or visit the archives