Drupal logo next to PDF icon.

With DrupalCon Global coming up, we’re reminiscing about some of our past presentations. Nashville is definitely in our top 5 list of best DrupalCon experiences. The conference was great, the city was beautiful, and the food was delicious! Our lead developer, Dan Hansen, gave a very informative presentation about creating PDFs in Drupal 8, and it’s still relevant today.

PDFs are a universal document format used to stabilize a document. For example, two people may open a Word document and see different fonts, styles, and formatting. With a PDF, everyone views the document the same way, the way it was meant to be viewed.

PDFs are based on Postscript (.ps), a language used to format documents for printers. They began as a proprietary format from Adobe in 1993. However, as recently as 2008, PDFs are recognized as an ISO standard. They can now be managed within Drupal just as easy as handling images. PDFs were designed to combine the Postscript-based document layout with font embedding and the data storage that would allow it to be compressible and easy to pass around.

PDFs have three unique features:

  1. Rich Media: When you look at a PDF, you can view various image formats, sometimes even videos.
  2. Security: It is possible to secure PDFs to an extent. You can encrypt a PDF and make it password protected so that only people you want to see the PDF can.
  3. Standardization: The document and layout are consistent among all viewers.

 

Creating PDFs

It’s important to ask yourself whether or not you need to generate a PDF in code. Chances are, you don’t. For example, creating PDFs on the backend is really unnecessary if you’re creating PDFs using the content pages of a website. Most modern browsers have effective Print-to-PDF methods built in. If there’s no need to code a PDF, users can simply use print style sheets to print PDFs.

When do you need to generate PDFs?

  • If you require precision layouts, like forms
  • If you need to implement a template
  • If you need to implement security features

 

PDF Rendering Libraries

Drupal logo pointing to PDF icon.

jsPDF

This is a client-side rendering library. This library can be used to render HTML with the addition of html2canvas or rasterizedHTML.

 

FPDF

This is a server-side library that is 100% PHP based.

 

mPDF

This is also a server-side library and is 100% PHP based. mPDF is actually based on FPDF and HTML2PDF. In contrast to FPDF, mPDF offers improved language and UTF-8 handling. However, this library is slower than the original tools. This comes as a result of the expanded feature handling and language abilities. Regardless, it’s a lightweight tool that you can use to achieve basic PDF generation efficiently.

 

TCPDF

Similar to FPDF and mPDF, this is another server-side library that is 100% PHP based. In fact, TCPDF draws a lot of its inspiration from FPDF. Currently, there’s a new version of this library in the works. This means that the current functional version is considered “obsolete” and will no longer receive feature updates. Still, this is a powerful library that does a great job of using HTML2PDF. It’s important to note that the old version does not work on PHP 7.2+ without patching.

 

FPDI

This is another server-side library that acts as a bonus for FPDF. It also works for TCPDF. This rendering library offers extra features including the ability to use PDF files as templates. For instance, you have the ability to pull a single page from a large PDF and use it to create a new PDF. There are also some FPDI extension libraries that can encrypt PDFs and attach other security features. However, if you’re trying to build a PDF above version 1.4, you’ll have to pay money. This is really only a problem if you’re receiving files from third parties who don’t have control over versions or feature sets.

 

DOMPDF

This is a common server-side library that is 100% PHP based. After a long static stint in Google code, this library is back to active development. It also cooperates with package management solutions like Composer. This is a powerful and widely-used library; however, there are a few issues including Render loop on large tables and no float support. These issues are well documented, and there are multiple resources that help you get around those issues. Officially, this library only supports CSS 2.1 and HTML 5.

 

Wkhtmltopdf

This is a Software Binary, server-side library. If you build sites and operating browsers that use WebKit, then you’ll have an easier time rendering PDFs with Wkhtmltopdf. Because it is Software Binary, you have to use an interface like PHP Wkhtmltopdf to work with Drupal or other PHP applications. This is a great option to use, but you’ll need to use the right version of the Binary. If you get the wrong version, you’ll need to run an ex-server or an ex-server simulator. There are a couple Composer-based libraries that you can use in Drupal 8.

 

PDFtk

This is another server-side library that is also Software Binary. This is more of a PDf manipulation tool that allows you to add and remove pages, combine PDFs, and fill PDF templates with data.

Other Rendering Libraries

  • html2ps/html2pdf
  • AdobeFOB
  • PDFLib
  • Zend_Pdf
  • XSL:FO

These are just a handful of additional rendering libraries. Most of these options may not have extensions related in Drupal or easy PHP implementations.

 

Drupal 8 PDF Modules

Printable

There’s now a printer, email, and PDF version of Printable for Drupal 8. This module was stabilized at the end of 2017. It relies on the PDF API module, which is not yet stable. This means that the API will not install without an older version of DOMPDF.

 

Entity Print 

This module has a flexible library selection. There are no version issues, and you can use current branches for all libraries. This is the best replacement for the old Print module.

 

FillPDF 

This module simply fills PDFs with data using PDFtk or a paid third-party service. It can greatly reduce the overhead of creating PDFs.

 

PDF Tips and Tricks

Here a few scenarios that Dan explores in his presentation. For these demos, Dan is using Drupal 8 on Docksal based on Composer.

  • Make a PDF from HTML
    • Use Entity Print with Wkhtmltopdf or DOMPDF. But what if you don’t want to build PDFs based on an entity?
  • Combine Two PDFs
    • The best way to go about this is to use PDFtk and a wrapper like mikehaert/php-pdftk.
  • Dump a Generated PDF into a File Field
  • Fill In a PDF Template
    • You can use FillPDF to do this. If you want to do this programmatically, use PDFtk and a wrapper like mikehaert/php-pdftk.


There’s still time to register for DrupalCon Global. DrupalCons, whether in an exciting new city or virtual, are always a chance to learn something new, make connections, and generally get acquainted with the Drupal community. If you’d like to be a part of the Drupal community, consider your local Drupal Camp. DrupalCamp Atlanta is going virtual this year as well. They’re currently taking submissions and offering early bird prices on registration. Get involved!

 

 

 

Free consultation to discover your best-fit solution.