Installing xhtml2pdf on OS X

Xhtml2pdf is a cross-platform Python module that allows easy conversion of HTML or XML to PDF files. Its dependencies, ReportLab & Pisa, also have a host of very useful functions, including programmatically creating or modifying PDF content & creating barcodes.

Of particular interest to us here at Beezwax is using it to work around the lack of PDF generation functions when running server-side FileMaker scripts. But the steps below should be useful for even non-FileMaker folks who wanted to use xhtml2pdf on their system. The install instructions are not hard, but due to a few minor complications, definitely not obvious to someone unfamiliar with Python’s package management tools.

 

But before we can have any fun using this, we’ll need to get it installed for us to use. Below I’ll go through installation steps, which I’ve tested on Mac OS Yosemite and Mac OS El Capitan.

1 – Install XCode command line tools

An easy way to install this is to open the Terminal and run  gcc . If the command line tools are not present, you will be prompted with a dialog and options to either install the full XCode tools, or just click Install for the command line tools only.

2 – Install Pip

This is a Python package manager that supplements easy_install. Newer Python installs include Pip by default, but not Apple’s version included with OS X. In particular, Pip knows how to build libjpeg, a required library. Without Pip we’d have to build this separately or fiddle with some paths in obscure xhtml2pdf files.

We use easy_install to bootstrap our pip based installs. Installation is simply

3 – Install xhtml2pdf

On Yosemite, this installs easily:

sudo pip install xhtml2pdf

On El Capitan, its new file system restrictions complicate the install, so we must install into a specified folder:

 4 – Let’s Test

First, startup Python in the Terminal.

For El Capitan, we’ll have to tell Python about our custom path:

Now paste in the following code. It should create the .pdf file and open it in Preview.

Epilogue

Now that we have it installed, in a later post I’ll discuss how to use it with server-side scripts in FileMaker.

A good starting point for finding out more about xhtml2pdf and Pisa is here: https://github.com/xhtml2pdf/xhtml2pdf/blob/master/doc/usage.rst

For ReportLab the main documentation site is http://reportlab.com/documentation/

Simon

0 thoughts on “Installing xhtml2pdf on OS X

    1. @zerobatsu,

      Yes, its a very useful library, so definitely deserves the promised follow-up.

      I’ve got a bit of a backlog at the moment, but will try to get to that done by end of August.

      Simon

Leave a Reply