Goodbye htmldoc, hello mPDF

Written 30/12/15

One of the things we've needed to do for WalkLakes is to generate PDF versions of our walks and I've talked here before about how we did this using htmldoc, a program which converts HTML to PDF (or Postscript), but that it had its limitation - mainly that it had no CSS support and was designed around an old release of HTML.

The other problems with htmldoc is that the code is flaky. The latest version distributed with Ubuntu is broken for images and building it from source last time was a struggle.

Which brings me to mPDF. I can't remember why I did a search on Boxing Day for generating PDFs from PHP but I did and that threw up TCPDF. So on Sunday, after a crappy night and a migraine, I started a rewrite of the code we use to generate PDFs of walks and was impressed: I very quickly made progress.

Meantime I read this blog post suggesting that mPDF might be a better option as it had better CSS support. But I stuck with TCPDF as that's what I'd started with ...

But then two things happened: I started to need CSS and the TCPDF web site went offline just as I needed to refer to the documentation so I thought I'd have a play with mPDF and the blog post was right: it suited me better. By the end of the day I'd pretty much got walks rendering as I wanted them to.

There were some minor niggles:

But all of these were things I could, and did, work around as the sample page on the right shows (click the image to view full size). By last night I was ready to put it live on WalkLakes and I've now done so. Among other things this means people purchasing our walks are now getting a PDF uniquely created for them ... which should mean we can more easily track where copyright violations originated.

The other thing we used htmldoc for here is the vast majority of correspondence like letters and invoices. We write them in HTML (with some tweaks) and then use htmldoc to render them. So today's main task has been to replace the rendering engine there with code based on mPDF running from a PHP CLI script and I now have that working too.

So the bottom line is that we've now removed the need to keep struggling on with htmldoc and we can also generate much prettier PDFs with the help of CSS.

And although mPDF was, like htmldoc, a one man development it's now on github where an active community of users are contributing tweaks which bodes well for its future.

Paul

« »