Effective PDF Generation in Drupal

Originally published at: http://www.sitepoint.com/effective-pdf-generation-drupal/

A few months ago I had a client requirement for PDF generation, in this case to generate certificates that could be viewed online or printed. I spent some time looking into the best Drupal options available and picked up some advice along the way on how best to accomplish these aims. After mentioning my results to several people, it seemed that PDF generation was a common requirement and now I have the same need again on a personal project, so it seemed a good case study to walk you through what I found.

Why not just print?

If your requirements are simple, it may be easier to just to tell your website users to print and there’s nothing stopping them doing this. If we want a level of control over what is printed or we want to distribute files for printing, then we need to look into other options.

Web vs Print

PDF generation takes a slight change of mindset. As web developers, we have spent a lot of time convincing designers from a print background to stop producing pixel perfect designs that will be difficult to reproduce on the web. If you want to introduce PDF generation or any form of high designed print output, then we need to relearn some of our old skills we left behind. The nature of print means that it is precise and often needs pixel (or millimeter) perfect design.

What am I trying to accomplish?

I am currently working on a board game and I want to allow players to be able to create their own cards that can be shared on the website and printed for use in the game. We have a specific size and layout that these cards will always be and need to conform to, here’s the initial design that we will partially recreate:

PDF options in Drupal

There are two options in Drupal for creating PDFs. The Print module and Views PDF. Views PDF initially seemed the better option as it would allow us to leverage the power of views and the myriad options it offers. However, it has the PHP module as a dependency and as far as I know, is reliant on the eval() function. PDF generation has the potential to be a server intensive task and this method seemed inefficient to me, aside from my reluctance to ever have any kind of PHP evaluation module enabled in Drupal.

This caused me to settle on the print module, which is also better supported and offers many other options for output that may prove useful.

Next we need to decide on our PDF generation library, I am going to suggest you use wkhtmltopdf and explain why later, as I want to build something to compare first. Do this by visiting the wkhtmltopdf website and follow the instructions for your setup. Remember it will need to be installed on local and production sites. After installing you need to create an alias to the wkhtmltopdf executable into your Drupal libraries folder, i.e.:

ln -s /usr/bin/wkhtmltopdf /var/www/sites/all/libraries/wkhtmltopdf

Continue reading this article on SitePoint

I had tried getting wkhtmltopdf working a few months ago; however, I was unable to get it working. Seemed to be a problem at the Drupal integration level since wkhtmltopdf worked at the command line level. I was curious whether you had to do any specific tweaking to get wkhtmltopdf working with Drupal (e.g. had to use specific version of wkhtmltopdf, etc.)?

Hmm, the only tweaking I did on the drupal side was the dpi and margins mentioned above. You sym linked the executable into the libraries folder?

I am using : wkhtmltopdf 0.12.1 (with patched qt)

Which is on a Vagrant box running Ubuntu 14.04

What’s your setup? And what errors do you get in logs?

If you want something that is much more scalable and without limitations of html5/css3 inclusions, i encourage you to take a look at docraptor third party service. It’s nominal cost and I created a plugin to the print module that works quite well. My issue with the other libraries were limitations in recognized styles as well as performance. I can send several hundred pages to docraptor with no issues. Everything else i tried failed on larger documents.

Thanks for that, I wasn’t aware of the option. Have you had any issues with replicating layouts?

I haven’t tried this in Drupal, but I was recently successful in using FPDF in WordPress. I assume it will be a little more challenging in Drupal, but I was more than happy with my WP results.

No problems in replicating layouts with Docraptor. FYI, docraptor runs on princexml. I chose docraptor because princexml as a standalone has to run as an executable on the server which i can’t do in a cloud hosting environment and its also very expensive for license. So, docraptor acts as a middleman to generate the pdfs on their end and return them to your site.

Additionally, you can create pdf bookmarks, dynamic table of contents, footers with page count, etc… You can see some pretty nice samples here: http://www.princexml.com/samples/. All of the supported css is listed on that site as well.

I’ve been battling this issue for years. I started with htmldoc, and then went to wkhtmltopdf. While the second was better than the first, both of them require setting up a binary on the server which means that the code is not portable, especially in a shared hosting environment.
I then tried PhantomJS, which is better again, and you can install the binary as part of your source tree, however it still isn’t all that great.

Which brings us to PHP libraries.
DomPDF had various issues, I struggled to get it working properly (as you did).

What I’m using now is mpdf, which I’ve found to be fantastic. It supports CSS properly (unlike wkhtmltopdf), is totally portable with your codebase, and to be honest I haven’t found any issues with it. It just works really well.

I’d really recommend trying it out.

Thanks for all your feedback everyone! I had mainly followed the path of what was listed on the print module page, but as I continue my project I may look into some of these other options.

Does anyone have any extra tips for getting layouts working well in ‘print’, I assume/hope that the article still applies with any of these other options and the CSS advice.

I had a similar requirement a few weeks back and decided to write a new module that was much smaller than Print. The module is called Entity Print and it uses wkhtmltopdf. The main advantages are:

  • It works with any entity type, not just nodes.
  • Much lighter, the whole module is less than a few hundred lines of code.

Thanks!

I remember coming across this module and there was a reason I didn’t pick it at the time, can’t quite remember why though. I’m wondering if it wasn’t stable when I was looking. May give it a go with this project instead and report back.

Yes it likely wasn’t tagged. I’d love some feedback if you have time to test it.

The main issue right now is the amount of setup phpwkhtmltopdf requires which I depend on.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.