The following post was written by Drupal Association Premium Supporting Partner, CI&T.

 

Have you ever been asked to export pages or content as a PDF by a client? Yeah, I know that feeling. I think this is one of the most underestimated development tasks in the non-technical world. It is not that easy, and it is even harder to explain it.

  • What tool should I choose? Is it compatible with the OS the site is hosted on?
  • Is there a module for that?

Ok, it’s not impossible, but it’ll take time. Probably more than what your client is willing to pay. And then the actual problem appears when you need to export that “example” form (that won’t be submitted), or a 100% JS quiz.

Generating that HTML on the server side, when the logic is on the client side, is hard to achieve. You’ll probably need to duplicate the logic so you can recreate the same scenario the end user has on their screen. But what is the point of doing that if we can extract exactly what the user is seeing with no effort?

That’s some of what the PDF Export module tries to solve, transforming this process into a few simple steps:

  • Download and install the module
  • Choose what library to use and download dependencies
  • Add a PDF Export button to your view/node/page
  • That’s it!

Look at the following sample of an HTML page exported into a PDF file using this tool:

Views Table exported with PDF Export module using Bartik theme including the print css file

Customization

You can just use the theme’s CSS, but it also offers some useful customizations:

  • CSS Files: Define multiple css files from your theme, also include a print.css file if you want.
  • HTML: Implement hook_pdf_export_html_alter(&$html, $filename) for a customized cover or url rewriting.
  • Selectors: You have total control of what is going to be exported, just enter the jQuery selectors you want to export.

Configuration form of the Views area plugin

Images not showing up because of your basic authentication? No problem, PDF export solves it for you.

Some PDF generators try to use http instead of simply looking for your files in the filesystem. So, you’ll have to use absolute urls, since you need to include your basic authentication credentials on the on the url.

PDF Export will do the hard job, enable the basic auth rewrite, and it will lookup the credentials from the $_SERVER variable, rewriting all the urls to the correct domain. In case you need anything else, or use another domain, you can simply configure it on the module:

PDF Export settings dialogue box

Upcoming enhancements

PDF Export is in active development, sponsored by CI&T. Stay tuned for the upcoming enhancements:

  • PDF Export: Node
  • Blocks with custom export PDF buttons
  • Integration with wkhtmltopdf

Don’t hesitate to post your own feature request on the module’s issue queue.


Sponsored content, like this post, is made possible by contributions to our Supporting Partner Program. Its program fees support the Drupal.org Engineering Team, which maintains, optimizes, and improves Drupal.org.