Usage

Last updated on
2 July 2025

The Drupal 8 version of the Entity Print module provides multiple methods of exporting entities to PDF(s) via PDF engines. We are actively working to support more export formats, currently Word (.doc) and potentially ePub afterwards.

Supported Engines

See the list of supported PDF print engines

Installation

  1. Enable the Entity Print module
  2. Note Entity Print has its own permissions that will need to be granted for non-admin users to download the PDF's

Additionally, for Views support, enable the Entity Print Views module.

Exporting Entities

Via Routes

The URL format for all entities is: http://example.com/print/pdf/[entity_type]/[entity_id] for example, http://example.com/print/pdf/node/1

(note, in a previous release of the module, the path was /entityprint as opposed to /print. Both variants are supported in the current version)

However, if you are exporting a View and want to apply a contextual filter, here is the best URL format:
http://example.com/print/view/pdf/[view_id]/[display_id]?view_args[]=YOUR_CONTEXTUAL_FILTER for example, http://example.com/print/view/pdf/my_certificates/block_1?view_args[]={{ uid }} (assuming this link is embedded in your View and you want to filter by user ID.

Debugging

Entity Print provides a HTML version of the entities sent to the PDF engine for easy and quick debugging .The URL is the same as above with /debug appended. E.g. http://example.com/print/pdf/[entity_type]/[entity_id]/debug

Entity Print adds a disabled field to the view modes of each content type on your Drupal 8 site. The field is titled Entity Print and has a default label value of "View PDF". If you want a link on all of your Articles, then make this field visible on the Manage Display page of the Article content type with the Default view mode (/admin/structure/types/manage/article/display), then you will see a link on every Article using the Default view mode. And, you can use this same process on any content type to achieve the same result.

Via Views Node Operations Bulk Form

Navigate to /admin/content. Select the nodes that you would like to export. Select "Download PDF" using the "With selection" node operations bulk form. Click Apply. You will download a PDF with each node printed on the PDF. We have an issue open to download multiple PDFs as a ZIP archive. When this is completed, you will be able to export one entity per file using the node operations bulk form and those files will be archived and downloaded as a ZIP file.

Create your own View

I will already assume that you have created a View of content entities and configured everything to your needs. Add a new Node operations bulk form field to your View. You set the Action title to whatever you'd like. To just export the entities to a pdf, select "Only selected actions" under "Available actions" and select "Download PDF" under "Selected Actions". Save it and go to your view, select the checkbox(es) next to the entities that you'd like to export, and click the Apply button.

Exporting Views

With the Entity Print Views module enabled, a "Print" Global option can be added to the View's header or footer.

Styling the PDF

You will notice right away that the "View PDF" link is also written to the PDF. If you would like to remove the "View PDF" link on the PDF export (say for the Article content type), go to the Manage Display page for the Article content type (/admin/structure/types/manage/article/display). Then you will have to click on the "Custom Display Settings" dropdown near the bottom of the page. This will reveal the view modes available. You will want to select the PDF option. This will enable the PDF view mode for the Article content type and give you a new tab "PDF" at the top of the Manage Display page. Click that "PDF" tab. Disable the Entity Print field on that PDF view mode page for the Article content type. If you export the PDF now, you will notice that the "View PDF" link is gone.

By default this module only installs the PDF view mode for Nodes. If you have any custom entities on your Drupal site and want to hide the "View PDF" link on those, you must first create a PDF view mode for the entity type. To do this navigate to "admin/structure/display-modes/view" and click on "Add view mode". In the list click the entity type for which you would like to create the PDF view mode. You'll now be asked to name the view mode, you can name it whatever you like but make sure the machine name is just "pdf", which will be automatically prefixed with the entity name. You can now follow the steps in the previous paragraphs for your custom entity.

From your theme

The recommended way to style your PDF's is from your theme. The following examples show how to register entity_print CSS files from your theme info file.

You can register CSS per entity type, per bundle or using the special "all" key which apply to all generated PDF's.

Example theme.info.yml

name: Entity Print Test Theme
type: theme
base theme: bartik
package: Entity Print
version: VERSION
core: 8.x
+core_version_requirement: ^8.7.0 || ^9

entity_print:
 node:
   # Adds article-pdf css library to the Article bundle on the Node entity.
   article: 'entity_print_test_theme/article-pdf'
   # Adds the all-nodes library to all node PDF’s.
   all: 'entity_print_test_theme/all-nodes'

For example, to add the CSS file css/print-style.css to all your nodes, add this in your YOUR_THEME_NAME.info.yml:

entity_print:
  node:
    all: 'YOUR_THEME_NAME/print-styling'

(Changing 'node' to 'view' to associate with Views instead).

In case if the modules library has a name, the library name should be pointed too (pay attention to the commerce_invoice module name):

# Extend base_theme libraries.
libraries-extend:
  commerce_invoice/entity-print-styling:
    - YOUR_THEME_NAME/print-styling

Add this to your YOUR_THEME_NAME.libraries.yml:

print-styling:
  version: VERSION
  css:
    theme:
      css/print-style.css: {}

Clear cache to register the changes, and debug at http://example.com/print/pdf/node/NODE_ID/debug.

Modifying entity content

To modify the content of your entity, you create a twig template using the 'pdf' view mode name (e.g. node--article--pdf.html.twig). In this template you can modify the markup as you normally do with other view modes using {{ content.field_example }} or {{ node.field_example }}.

Modifying pdf template

You can also modify the HTML output of the entire Entity Print nodes and PDFs. To do this, you will have to create an entity-print.html.twig or entity-print--node--[content-type].html.twig in your custom theme. Only {{ content }} (8.x-1.x: {{ entity_array }}) is available to render the content.

You have to make sure the {{ entity_print_css }} is included inside the <head> tag in your custom Twig template file in order to make custom CSS libraries working.

Attach a css library from a custom module via an EventSubscriber

You can attach a library from your custom module using an Event Subscriber listening for the PrintEvents::CSS_ALTER event.

  • The following was lifted from Webform's Webform Entity Print submodule

Create the Event Subscriber

<?php

namespace Drupal\custom_module\EventSubscriber;

use Drupal\entity_print\Event\PrintCssAlterEvent;
use Drupal\entity_print\Event\PrintEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber to alter entity print css.
 *
 * @see \Drupal\entity_print\Asset\AssetCollector::getCssLibraries
 */
class CustomModuleEntityPrintCssAlterSubscriber implements EventSubscriberInterface {

  /**
   * React to a config object being saved.
   *
   * @param \Drupal\entity_print\Event\PrintCssAlterEvent $event
   *   Entity Print CSS alter event.
   */
  public function alterCss(PrintCssAlterEvent $event) {
    $entities = $event->getEntities();
    foreach ($entities as $entity) {
      if ($entity->getEntityTypeId() === 'entity_type_id_here') {
        $event->getBuild()['#attached']['library'][] = 'custom_module/library_name';
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      PrintEvents::CSS_ALTER => 'alterCss',
    ];
  }

}

Register the event subscriber

services:
  custom_module.print_css_alter_subscriber:
    class: Drupal\custom_module\EventSubscriber\CustomModuleEntityPrintCssAlterSubscriber
    tags:
      - { name: event_subscriber }

Here's a simple example.

Say you have a custom module called cwl. And you want to use it to change the font in the pdf-files to Montserrat.
Sounds deceptively simple.

Here's the best way to do it:

First you might want to disable entity-print.css completely - you never know what'll be put there within the next decade.

1. Make sure you have a file called
/modules/custom/cwl/cwl.module

Make sure it at least contains something like this:

/**
* Implements hook_css_alter().
*/
function cwl_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
// Remove the entity-print.css from the equation
$module_path = \Drupal::service('extension.list.module')->getPath('entity_print');
$file_path = $module_path . '/css/entity-print.css';

if (isset($css[$file_path])) {
unset($css[$file_path]);
}
}

2. Place your fonts at

/modules/custom/cwl/css/fonts/Montserrat-Regular.ttf and /modules/custom/cwl/css/fonts/Montserrat-Bold.ttf
You'd probably use google-webfonts-helper to get the fonts.

3. Make sure that you have a file called
/modules/custom/cwl/css/cwl-print.css

Make sure it at least contains something like this:

@font-face {
font-family: 'Montserrat';
src: url('/modules/custom/cwl/css/fonts/Montserrat-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}

@font-face {
font-family: 'Montserrat';
src: url('/modules/custom/cwl/css/fonts/Montserrat-Bold.ttf') format('truetype');
font-weight: bold;
}

body {
font-family: "Montserrat", "DejaVu Sans", Helvetica, Arial, sans-serif;
}

4. Make sure you have a file called
/modules/custom/cwl/cwl.libraries.yml

Make sure it at least contains something like this:

cwl-print:
version: 1.x
css:
theme:
css/cwl-print.css: {}

Note: do remember that Drupal will punish you severely, if you botch the indents in the yml file.

5. Make sure you have a file called
/modules/custom/cwl/cwl.services.yml

Make sure it contains something like

services:
cwl.entity_print_css_alter_subscriber:
class: Drupal\cwl\EventSubscriber\cwlEntityPrintCssAlterSubscriber
tags:
- { name: event_subscriber }

6. Make sure you have a file called
/modules/custom/cwl/src/EventSubscriber/cwlEntityPrintCssAlterSubscriber.php

Make sure it contains something like

<?php

namespace Drupal\cwl\EventSubscriber;

use Drupal\entity_print\Event\PrintCssAlterEvent;
use Drupal\entity_print\Event\PrintEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Event subscriber to alter entity print CSS.
*
* @see \Drupal\entity_print\Asset\AssetCollector::getCssLibraries
*/
class cwlEntityPrintCssAlterSubscriber implements EventSubscriberInterface {

/**
* React to a config object being saved.
*
* @param \Drupal\entity_print\Event\PrintCssAlterEvent $event
* Entity Print CSS alter event.
*/
public function alterCss(PrintCssAlterEvent $event) {
$entities = $event->getEntities();
foreach ($entities as $entity) {
if (TRUE || $entity->getEntityTypeId() === 'a_specific_content_type') {
$event->getBuild()['#attached']['library'][] = 'cwl/cwl-print';
}
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
PrintEvents::CSS_ALTER => 'alterCss',
];
}

}

All set?

Flush the caches (on Drupal 10 that's at /admin/config/development/performance - but better get the Devel module)

The next time you view a pdf produced by entity_print, it should have changed the font to Montserrat!

One user's case

After about a month of researching how to use the "Entity PDF" module, I've consolidated all my findings in one place to help anyone who needs this module as well.

This is my scenario. I need to create a PDF with a custom style for issuing training certificates in courses. This PDF will be printed and has a different style than the rest of my website's theme. I created a content-type "certificates".

After installing the module, create a new "view mode" called "PDF"
It is in the path "/admin/structure/display-modes/view"

Access the content-type that will need to generate the PDF.
In my case: /admin/structure/types/manage/certificates/display/pdf

Move the "View PDF" field from "disabled fields" to the list of visible fields.

Copy the file "entity-print.html.twig" from "modules\contrib\entity_print\templates"

Paste the file into your theme. In my case, it is in "themes\custom\subtheme_barrio\templates\content\entity-print.html.twig"

Create a new file "node--certificates--pdf.html.twig" in "themes\custom\subtheme_barrio\templates\content\node--certificates--pdf.html.twig
This is the file that should be customized according to your needs

If you need to use fields, you can use this format:

{{ content.field_user.0['#title'] }} - if it is a taxonomy
{{ content.field_trainning_date.0['#value']|date('d/m/Y') }} - if it is a smartdate field

In the file "themes\custom\subtheme_barrio\subtheme_barrio.info.yml", add this information:

entity_print:
 node:
   # Adds the all-nodes library to all node PDF’s.
   all: 'subtheme_barrio/print-styling'

In the file "themes\custom\subtheme_barrio\subtheme_barrio.libraries.yml", add this information:

print-styling:
version: VERSION
css:
theme:
css/print-style.css: {}

Create a file "print-style.css" in this folder: "themes\custom\subtheme_barrio\css\print-style.css"
Style this file according to your needs

I hope it helps!

Help improve this page

Page status: No known problems

You can: