Batch convert HTML to PDF on MacOSX

Recently I needed to convert well over 200 HTML files to PDF, well normally converting to PDF on the Mac is easy, just press print and choose “Save as PDF”, but doing that for 200+ Files seems kind of a waste of time. Beeing a Mac user for quite some time now, I know about Automator and the batch print support so here my Ideas:

Speed things up by creating an Automator action, but there is now way to do this! I found some “Convert to PDF” action but it only works for text files.

Put the printer in the dock and drag all files on it to batch print them. But guess what there is no PDF printer, and the only one I found online (besides Adobes, which is included in the CS) does not work with Snow Leopard.

Well so much for the easy Mac like ways of doing this, and back to the underlying Unix. Bash and Macports to the rescue. Searching Macports (actually searching FreeBSD ports and finding it to be availible on Macports, too) I stumbled across Htmldoc. It takes HTML files (including Images etc.) and converts them to PDF. After installing

sudo portinstall htmldoc

I was just one line of Bash away from my nicely formatted and portable

find . -name “*.html” -exec htmldoc —webpage -f {}.pdf {} ;

So what does it do?

  • Find all files with .html extension in the current Folder
  • Run htmldoc expecting a webpage
  • Outputting a file named like the in HTML file but with added .pdf extension
  • Run on all files matching the *.html expression

So there it is, ever want to convert a lot of HTML to PDF, just put it in one folder and run this line.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s