Skip to main content

Printing any Drupal Entity to PDF

by benjy /

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

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

   # 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.

Posted by benjy
Lead Drupal Developer





Hi Ben, I was trying to follow your article as wanted to generate pdf from html for my custom entities but after installing the module when went to the /entityprint/[entity_type]/[entity_id] path I got an error

Fatal error: Class name must be a valid object or a string in on line 7999.

Can You please help me that where I'm going wrong

Comment by Gerhard Groenewald


Hallo Ben,
my website is hosted on a shared server, I cannot run "composer require "dompdf/dompdf:0.7.0-beta3" or drush.
Is there a way that people who make use of share hosting can install "Entity Print" with Drupal 8?
I've searched for many hours for a solution but without any success...


Comment by benjy


You can run the composer require command locally first and then just upload the vendor folder to the shared server. The generated files are standard PHP files.

Comment by Gerhard


Hallo Ben,
Thank you very much for your reply, I do appreciate it very much..

I have one problem: My website is on a share hosting server.
I've got no idea how to run any command locally - don't I need a command prompt or something?
(I use Webdisk, Cpanel or Filezilla to manage files or update/upgrade Drupal on my website).

Your help will be greatly appreciated.
Kind regards

Comment by benjy


Yes, you'll need to run composer install from a command line.

Comment by suresh kumara


Thank you ben,

nice blog to read.

Comment by Hannes


Can you show how to style the link "View PDF". I see that there are attributes set in PrintLink.php. But I want to change attributes twig way. Any Idea how? Thanks!

Comment by kim.pepper


Please post support requests in the Drupal project issue queue

Comment by Thomas


Hello Ben,

I'm using the Entity Print module to export D8 views to PDF. But it's working wastefully, there is just one column of content in the exported PDF, so the portrait aligned PDF has only its half filled with text, and the another half is whitespace, which is waste in real printing. I would like to ask you, how can I modify the export pattern? It would be nice to have two columns for example and the rows are more compressed. I've got an advice to use the entity-print.html.twig file to to achieve the desired result, but I don't know how.

Thanks in advance.


Comment by kim.pepper


Please post support requests in the Drupal project issue queue