Working with PDFs in Drupal 8: Top Rendering Libraries, Drupal Modules & Useful PDF Tricks

Working with PDFs in Drupal 8: Top Rendering Libraries, Drupal Modules & Useful PDF Tricks

If only working with PDFs in Drupal 8 was as easy as handling images in media libraries...

Now, who's with me for a "PDFs in Drupal core" initiative?

No need to "despair", though: if you need to generate and handle PDF files on your Drupal 8 website, you still have a handful of tools (meaning useful PDF Drupal modules), Composer and PDF generating libraries at hand.

In this respect, inspired by an older Drupalcon session on "PDFs in Drupal" and Mediacurrent's synthesizing blog post on this topic, I've decided to put together some sort of compilation of the best available tools.

Those that make handling PDFs in Drupal 8 surprisingly straightforward.

1. Being Able to Create PDFs in Drupal: Why Is It Vital?

There are 3 heavy-weighting answers to this question, which are also the very reasons why you started reading this post in the first place:

  1. because PDF is a universal, platform-agnostic document format
  2. because it supports rich media
  3. because “standardization” translates into a higher level of security

2. PDF Rendering Libraries to Consider When Working with PDFs in Drupal 8

There was a "warning" question in the YouTube video of that DrupalCon session that I've just referred to, that... got me thinking:

"Are you really sure you need to generate PDFs in code?"

You could still rely on modern browsers' built-in Print-to-PDF functionality, you know. A functionality that comes along with full control over the print style sheets...

Do think about that before rushing to create PDFs in code...

You should consider generating PDFs only under the following exceptional circumstances:

  • you need to implement some critical security features
  • you're working with precision layouts 
  • you need to implement a template

That being said, here are some of the most widely-used PDF rendering libraries, ranging from server-side to client-side, to libraries that support the use of PDF files as templates, etc.

  • FPDI
  • Print-to-PDF
  • FPDF
  • PDFtk
  • Wkhtmltopdf
  • mPDF
  • jsPDF

3. PDF Rendering in Drupal 8: Modules at Hand

But before I jump to "exposing" you the 3 most useful PDF modules in Drupal 8, let me briefly mention their Drupal 7 correspondent, so to say: The Print module, the most popular tool for creating PDFs on Drupal 7 websites.

Now, getting back to Drupal 8, here are the available tools:

3.1. FillPDF

  • it enables filling in your templates with values
  • it can be easily integrated with a paid third-party service or paired with the PDFtk library

Why would you use it? Because it dramatically reduces the overhead of rendering your PDF files.

3.2. Printable

Built upon the Print module to enable PDF rendering in Drupal 8, it uses the PDF API, which is not yet stable.

Note: it still requires the older version of DOMPDF to... work its magic.

3.3. Entity Print

The essential module to use when working with PDFs in Drupal 8! 

Here's why:

  • it empowers you to print any View or Drupal entity to PDF
  • it's lightweight (as compared to the Print module)
  • it's conveniently flexible when it comes to PDF rendering library selection

4. Useful PDF Tricks That You Can Pull of in Drupal 

OK, so we've passed in review the tools at hand that make working with PDFs in Drupal 8 conveniently simple and effective. 

But how do you use them precisely? So you can handle your PDF files (more) efficiently...

In short: what cool PDF tricks can you do in Drupal 8?

Here are the 3 most useful PDF tricks that you can pull off in Drupal:

4.1. Merge 2 PDF Files

How? By using this combo:

A PDFtk library & a wrapper like mikehaertl/php-pdftk.

And here's how the code supporting this "trick" would look like:

$pdf = new Pdf([ 
  'A' => '/path/file1.pdf', // A is alias for file1.pdf 
  'B' => ['/path/file2.pdf','pass**word'], // B is alias for file2.pdf ]);

4.2. Fill in a PDF Template

This one's plain straightforward. You only need to "unlock" the FillPDF module's power:

$pdf = new Pdf([‘PATH_TO_PDF’]);
  ‘name_of_text_field’ => ‘Some value’

4.3. Generate a PDF from HTML

Just rely on an Entity Print & DOMPDF or on an Entity Print & Wkhtmltopdf combo for pulling off this PDF "trick".

Now, if it's off an entity that you want to create your PDF file, you can always... put together a custom controller!

One that generates this output:

$dompdf = new Dompdf();
// Pass the HTML markup.
// Render the HTML as PDF.
// Stream the generated PDF back to user via browser.

The END! These are the top tools, tips, and tricks to rely on when working with PDFs in Drupal 8. 

How about you? What other solutions have you found for solving various PDF handling challenges in Drupal?