03 May

Automating HTML to PDF conversion on OS X

Ever wondered how to convert a big batch of html files into PDFs?

Well, you *could* open each html file in the web browser of your choice and save/print it as a PDF. However, that’s going to take a long time for a big batch. One fun way you can do it in macOS is by creating an Automator service to do the donkey work for you. Let’s take a look at how to do that.

First, launch Automator (you’ll find it in your Mac’s Applications folder) and create a Service document (NB. Click any of the screen shots here to enlarge them).

Create Automator Service

Next, set the services input options as shown below.

Screen Shot 2016-05-03 at 14.09.55

Next drag in a Run Shell Script action. Make sure you set it’s “Pass input” option to “as arguments” – that’s important! This lets us pass the files we select as an input to the shell script.

Screen Shot 2016-05-03 at 14.10.33

Change it’s contents to:

for theFileToProcess in "$@"
do
cupsfilter "$theFileToProcess" > "${theFileToProcess%.*}.pdf"
done

And save it as “HTML 2 PDF”, or whatever makes sense to you. Now it should look like this:

Run Shell Script options

Now, if you select a one (or more!) HTML files in in Finder whilst holding your control key down, you should be able to find the service you just created under “Services”. as shown below. Select it and you’ll run the shell script on all the files you selected!

Screen Shot 2016-05-03 at 14.13.29

Note that this will work best on fairly simple html – if you have heaps of JavaScript, movies etc in your html then the PDFs may not be great. However, its a handy quick way of taking some of the pain out of converting html to pdf.

While we have your attention… why not check out our macOS apps?

Barcode Basics – macOS barcode generator (including Automator support)
Ai Actions – Automator action pack for Adobe Illustrator
Pages Automator Actions – Automator action pack for Pages