Skip to main content
  1. Posts/

PDF Generation in Drupal

·234 words·2 mins·
Drupal 7 Drupal 8 Drupal Drupal Development Drupal Commerce Drupal Performance

It is nice when a user can click a button and generate a PDF. Over the last few months, I have added PDF generation to many projects. Examples of PDF generation include order invoices, sell sheets, webform submission results, and general node to PDF generation.

When tasked at generating PDFs, I have been applying the dompdf library. The dompdf library implements the most versatility by using HTML and CSS to generate PDFs. Therefore, giving the most flexibility for custom theming.

Example 1
#

The most recent PDF generation task I completed was for Drupal commerce order invoices. I leveraged twig templates to provide as much flexibility as possible, which allowed me to incorporate a special entity view mode and for additional block regions. The new entity view display allows website admins to add, remove or rearrange order fields, and the additional block regions allowed for adding additional blocks throughout the template. The flexibility of the Drupal interface not only provides a way for website admins to generate order invoices but also allows them to modify the layouts of the PDF invoice.

Example 2
#

In a past Drupal 8 commerce project, I have also automated the process to generate picket tickets upon orders reaching a specific state in the commerce order workflow.

The dompdf library has been enjoyable and straightforward to use and I intend to use this library for future PDF generation projects.

Michael Michalak
Author
Michael Michalak
Experienced Drupal consultant and Fractional CTO with over 13 years of expertise in backend development, optimization, and securing Drupal applications, specializing in custom module development, migrations, and enterprise integrations across various industries.