Creating a dynamic PDF file is one of the most important points of a project. The crucial thing is to find the right solution that meets your expectations and requirements. In this article, we’ll show you one of the most popular PDF generation modules for Drupal that will be helpful for you to generate, view or download reports, articles, and invoices in PDF.
The first version of the module was released on 22 January 2015, the latest update - 18 June 2020.
The module can be installed on:
All stable releases for this project are covered by the security advisory policy.
According to the usage statistics from the module's page, it’s currently used by around 12 thousand websites.
The module is created and maintained by benjy.
Entity Print allows you to print (save) any Drupal entity (Drupal 7 and 8) or View (Drupal 8+ only) to a PDF file. It uses the PDF engines based on popular PHP libraries like Dompdf, Phpwkhtmltopdf and TCPDF.
composer require "drupal/entity_print 2.x"
composer require "dompdf/dompdf:0.8.0"
composer require "mikehaertl/phpwkhtmltopdf ~2.1" composer require "tecnickcom/tcpdf ~6"
Let’s check what this module allows us to configure.
The preferable PDF engine is Dompdf, which is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It’s a style-driven renderer so it’ll download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
After the module is configured properly and all permissions are set, we can start exporting entities and views to a PDF file.
Entity Print adds a disabled field to the view modes of each content type. The field has a default label value of "View PDF". To make this field visible on any content type, enable it on the Manage Display page
From now, we’ll have a view PDF button added to our content type. The URL button is:
a PDF file" width="782" height="1052" />
Here is the PDF we want to save or print:
a PDF file to save or print in the Entity Print Drupal module" width="1041" height="753" />
With the Entity Print Views module enabled, a "Print" Global option can be added to the View's header or footer.
The URL button looks like:
For easy and quick debugging, Entity Print provides us a HTML version of the entities sent to the PDF engine. The URL looks pretty much the same as above only with /debug appended.
By default, the "View PDF" link is also added to the PDF. To remove it, go to the Manage Display page for the specific content type. On the Custom Display Settings section you need to enable the PDF view mode.
Now you can disable the Entity Print field on that view mode. Next time you export the PDF, you won’t have the "View PDF" link included.
By default, the PDF view mode is installed for Nodes only. In case you have any custom entities, you must first create a PDF view mode for the specific entity type via "admin/structure/display-modes/view". You can name it whatever you like but remember that a machine name should be "pdf", as it will be automatically prefixed with the entity name.
The following examples show how to register entity_print CSS files from your_theme_name.info.yml file. You can do it for any entity type or even view.
#Add css library to all nodes:
entity_print: node: all: 'YOUR_THEME_NAME/print-styling'
#Add css library to the Node entities but only article bundles:
entity_print: node: article: 'YOUR_THEME_NAME/print-styling'
#Add css library to all views:
entity_print: view: all: 'YOUR_THEME_NAME/print-styling'
Don’t forget to define a CSS library in your YOUR_THEME_NAME.libraries.yml:
print-styling: version: VERSION css: theme: css/print-style.css:
All the normal ways to override templates are available. Using theme hook suggestions, you can create a Twig template and modify the content of any specific entity (e.g. node--blog-post--pdf.html.twig). In this template, you can modify the markup as you normally do by using > or >.
You can modify the HTML output of the entire Entity Print template. Just copy the entity-print.html.twig file from its base location into your theme folder. Using theme hook suggestions, you can also create entity-print--node--[content-type].html.twig file.
Make sure the > is always included inside the tag in your custom Twig template file. Otherwise your custom CSS libraries won’t work.
It’s worth mentioning that the Entity Print PDF engines are pluggable. It means you can easily implement your own engines. To do so, you need to create a class in your_module_name/src/Plugin/EntityPrint/NewPdfEngine.php that implements Drupal\entity_print\Plugin\PrintEngineInterface with its all required methods. After that, your plugin will be available in the engines select list on the configuration page.
There are a few other options for making a PDF of an entity in Drupal that you might want to investigate.
PrintFriendly & PDF is a plugin for Drupal 7, 8 and 9. Below you can see its features list:
Printer and PDF versions for Drupal 8+ is a module that also works for Drupal 9. It allows you to generate the following printer-friendly versions of any node:
The Entity Print module provides multiple methods of exporting entities to PDFs via different PDF engines. It is a very flexible and customizable module which will help you in dynamic generation of your articles, invoices or other content related to your Drupal website. The module has full test coverage and is ready to be used in production for Drupal 7, 8 and 9.