Lead Drupal Developer
Printing any Drupal Entity to PDF
In Drupal 7 the most popular module for converting a Drupal node to a PDF was the Print module but late on in the Drupal 7 release cycle I wrote Entity Print, a solution that allowed you to print not only nodes but any Drupal entity to a PDF.
Entity Print in Drupal 7 was great for printing Drupal Commerce order entities, Entity Form’s or even custom entities and using the template system and wkhtmltopdf the PDF’s were easily configured and styled.
The Drupal 8 version of Entity Print is now stable and ready for production, it has feature parity with the Drupal 7 version and a few improvements.
- PDF Engines are now pluggable, it supports Dompdf and wkhtmltopdf out of the box. Note, Dompdf is a pure PHP implementation of HTML to PDF so is much simple to setup.
- The D7 module has a prototype of a views integration that wasn’t ready for production, in Drupal 8 we have a Views Bulk Operation plugin for exporting multiple entities to PDF. Note, this is only available for nodes right now because of a core limitation, see https://www.drupal.org/node/2011038
Installing The Module
To install the module you’ll need to ensure that you have one of the supported PDF engines available. The easiest one to configure is Dompdf, you can install this with composer via:
composer require "dompdf/dompdf:0.7.0-beta3"
Once the module is installed, if you have the appropriate permissions you’ll immediately be able to view any Drupal entity as a PDF by visiting /entityprint/[entity_type][entity_id]
The “default” display for the entity will be used by default but if a PDF view mode exists, that will be used instead. Entity Print creates a PDF view mode upon installation for the node entity type but you can simply create new view modes for any entity type in the Drupal 8 UI.
To make it easier for you to expose the PDF, an extra field will be added to manage display for all entity types. Dragging this field into the “enabled” fields on the desired view mode will render a link to the PDF with the text configured on the display.
Styling your PDF's
Adding custom stylesheets is simple and can be done in a number of ways. The easiest way is from your theme, you can simply add an existing CSS library to the PDF, here are a few examples from my Entity Print Test Theme.
name: Entity Print Test Theme type: theme base theme: bartik package: Entity Print version: VERSION core: 8.x entity_print: node: # Adds the article-pdf css file only to the Article bundle on the Node entity type. article: entity_print_test_theme/article-pdf # Adds the all-nodes library to all node PDF’s. all: entity_print_test_theme/all-nodes
The “article-pdf” library contains simply body { background: red; }
as shown here:
The HTML is generated using the entity view mode and all the normal ways to override templates are available. If you must control the entire PDF template, you can take a copy of entity_print/templates/entity-print.html.twig and copy it to your theme.
Writing New PDF Engines
As stated at the start, Entity Print PDF engines are pluggable, so you can easily provide your own implementations. To do this, create a class in your_module/src/Plugin/EntityPrint/YourPdfEngine.php that implements Drupal\entity_print\Plugin\PdfEngineInterface
You can then fill out the required methods, I won’t go into detail here as there are already two pretty simple examples to copy from in Entity Print but feel free to create an issue in the queue if you get stuck.