August 27, 2013

The state of PDF printing with the Print module

Updated Wednesday, September 30, 2015

One frequent request we receive from clients is the ability to output certain content on their sites (for example product spec sheets, reports, lists or pages) as a PDF file. The Print module has been our trusty go-to for a long time and the module maintainer has done a great job ensuring it's compatible with a number of PDF generating libraries, but if you've never been tasked with setting it up you might be wondering which library to use. In this blog post, I'll run through some of the advantages and disadvantages of each.

The sample content

Recently, I had to implement the PDF output of a fairly large report, consisting of 30 tables with 7 columns each and 900 rows in total. This may be a fairly atypical example, however it gave me the opportunity to push the libraries to their limits due to the complexity and size of the output. This was also tested on dedicated server where I had full access to the PHP configuration for tweaking.

dompdf

For a long time, dompdf has been the default we've gone with for PDF support, due to its CSS support and reasonable performance. Unfortunately, the content I was aiming to print brought dompdf to its knees, even with PHP's memory_limit and max_execution_time increased to unreasonably high values. A quick glance at the issue queue indicates there might be some problems handling tables, however it is still a capable library which we still use on several sites.

TCPDF

TCPDF is a library which is seeing frequent updates and improvements. Unfortunately its CSS support is a bit lacking compared to dompdf. However to its credit TCPDF was capable of outputting the example content, although the formatting was visibly broken. Hopefully support will continue to improve, however given the functionality of other libraries it's hard to recommend TCPDF at the moment.

mPDF

mPDF is a library which doesn't have a long history of support by the Print module and is only available in the 7.x-2.x version, which unfortunately does not have a stable release yet. With that said, mPDF was capable of outputting a PDF of the sample content in a reasonably quick time and with accurate layout and good CSS support. If using the 7.x-2.x version of the Print module is an option, definitely consider giving the mPDF library a try to see if it works for you.

wkhtmltopdf

wkhtmltopdf isn't actually a PHP library, rather it is a program capable of using the Webkit rendering engine (what the browser Safari uses to display web pages) to output the PDF. While it sounds a little scary to set up and generally requires you to have root access to the server you host your website on, it's actually relatively simple. wkhtmltopdf can be simply downloaded for a number of platforms. Rather than installing the entire X11 system on your server (which is wasteful, given most web servers do not have a physical display attached to them) you can install Xvfb to simulate a screen and run wkhtmltopdf. This results in fairly quick and very accurate PDF generation which also has the advantage of not being constrained by PHP process limitations (such as memory_limit).

DocRaptor

DocRaptor is a 3rd party service, which integrates with the Print module to handle the generation of PDFs. It will load your site's CSS files to style the PDF and supports the CSS Paged Media specification to provide advanced functionality such as headers and footers. In testing, DocRaptor handled our large HTML table test-case as well as any other solution. If you're searching for a solution which is quick to set up consider DocRaptor, however be aware of the number of PDFs that will be printed from your site as the billing is subject to quotas and overage.

Conclusions

Given each library has its pros and cons it's hard to recommend one over another for every circumstance and they should be evaluated according to your use case. For example, if you have full access to your server and don't mind installing some packages you might want to look at wkhtmltopdf. Or if you're still on the 1.x version of the Print module, dompdf is probably your best choice, however if you're able to make the leap to the 2.x version, consider trying mPDF.

Feel free to share your experiences in the comments.