There were a lot of amazing sessions at DrupalCon Nashville 2018, but one of the few sessions that sparked my interest was “PDFs in Drupal” presented by Dan Hansen. In this session, Dan goes through the importance of PDFs, gave a short introduction to some of the more popular PDF rendering libraries, and gave a demo on some tips and tricks that I found very useful for my future projects.
Most, if not all of us, have opened a PDF recently. PDFs are popular because they are universal as a document format and can easily be sent to others without having to worry about whether their machine can open them. Despite this, Dan notes that it feels like PDFs are behind in support, and it would be nice to have better PDF handling in Drupal core - similar to images in media libraries.
PDF Rendering Libraries
This session introduced a handful of popular PDF rendering libraries:
PDFs in Drupal
- Printable - based on the Print module to allow generation of PDFs. It relies on the PDF API, which is currently not stable.
- Entity Print (recommended) - allows for printing any Drupal entity or View (D8 only) to PDF. This module provides flexibility with PDF rendering libraries and is more lightweight compared to the Print module and has a stable release for both D7 and D8.
- FillPDF - allows for filling PDF with values. This module can be used with the PDFtk library or a paid third-party service, and can help in reducing overhead of rendering PDFs.
Tips and Tricks
I found Dan’s demos to be the most interesting - as he showed some code examples of various (and seemingly common tasks) related to PDFs. The following examples from Dan’s session shows how simple and straightforward it is to work with PDFs:
Making a PDF from HTML
A custom controller can simply return the following output:
$dompdf = new Dompdf();
// Pass the HTML markup.
// Render the HTML as PDF.
// Stream the generated PDF back to user via browser.
Combining 2 PDFs
Using the PDFtk library:
$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 ]);
Notice that you can specify a password for the PDF file (if there is one). You can also extract specific pages from the PDF files as well:
$pdf->cat(1, 5, 'A') // pages 1-5 from A
->cat(3, null, 'B') // page 3 from B
->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East
->cat('end', 3,'A','even') // even pages 3-end in reverse order from A ->cat([2,3,7], 'C') // pages 2,3 and 7 from C
More of these examples can be found at https://packagist.org/packages/mikehaertl/php-pdftk.
Fill in a PDF Template
Using the FillPDF module:
$pdf = new Pdf([‘PATH_TO_PDF’]);
‘name_of_text_field’ => ‘Some value’
I really enjoyed and learned a lot of useful tips from Dan’s session, and I encourage anyone who is looking to work with PDFs in Drupal to check out the session.
Accessibility: Let's Talk PDFs | Blog
Top Drupal 8 Modules | Blog
Mediacurrent Top Drupal 8 Modules: Drupalcon 2018 Edition | Blog