Usage

Last updated on
7 September 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

This page provides documentation for the Entity Print module.

Initial Setup

  1. Make sure you have wkhtmltopdf installed, this module presumes the path is: '/usr/local/bin/wkhtmltopdf' you can change this at /admin/config/content/entityprint
  2. Make sure you have the entity print view permission.
  3. Install https://www.drupal.org/project/phpwkhtmltopdf
  4. Install PHPWKHTMLTOPDF dependencies: drush make --no-core -y sites/all/modules/phpwkhtmltopdf/phpwkhtmltopdf.make

*Please note*
The wkhtmltopdf binary must be installed on the server. This module will not work unless you're able to upload/install the tool and make it executable. Some shared hosts may not allow this (only ftp access). If you have ssh access it might work, depending on the host.

Generating the PDF

The URL format for all entities is: example.com/entityprint/[entity_type]/[entity_id]

Examples

A few common examples are shown below.

  • Print Node 1 - example.com/entityprint/node/1
  • Print Order 3 - example.com/entityprint/commerce_order/3

Debugging

Entity Print provides a HTML version of what is 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/entityprint/[entity_type]/[entity_id]/debug

Styling the PDF

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 provide a global CSS file across all PDFs using the special "all" key or you can provide CSS files that are applied to the generated PDF per entity type and bundles.

; Applies to all PDF files.
entity_print[all] = 'css/styles.css'
 
; Applies to all bundles on the commerce order entity type.
entity_print[commerce_order][all] = 'css/orders-pdf.css'
 
; Applies only to article bundles on the node entity type.
entity_print[node][article] = 'css/article-pdf.css'
; If you want to use entity_print_views submodule(alternate to views pdf).
entity_print[views][all] = 'css/<yourcustom>.css'

From your module

If you want to add custom CSS files to a PDF from a module you can do that with hook_entity_print_css().

<?php
/**
 * Implements hook_entity_print_css().
 */
function MY_MODULE_entity_print_css($entity_type, $entity) {
  // Adds pdf.css to all commerce_order PDF's.
  if ($entity_type === 'commerce_order') {
    entity_print_add_css(drupal_get_path('module', 'MY_MODULE') . '/pdf.css');
  }
}
?>

Overriding Templates

You can copy the entity-print.tpl.php template into your theme to take control of the global PDF template.

If you wish to have per entity type templates you can do so by implementing hook_theme() for different entity type keys, for example:

<?php
function MODULE_theme($existing, $type, $theme, $path) {
  return array(
    'entity_print__commerce_order' => array(
      'base hook' => 'entity_print',
      'template' => 'entity-print--commerce-order',
    ),
  );
}
?>

Overriding Page Size

<?php
/**
 * Implements hook_entity_print_pdf_alter().
 */
function HOOK_entity_print_pdf_alter(WkHtmlToPdf $pdf, $entity_type, $entity) {
  $pdf->setOptions(array(
    'page-size' => 'A5',
  ));
}
?>

Help improve this page

Page status: No known problems

You can: