Drupal 8 batch PDF export

Reading time: 2 minutes.

Last modified: 24 May 2018

We have developed various Drupal 7 PDF related modules, but recently we made interesting Drupal 8 solution.

Workflows implemented: export one node in separate report, export up to 600 nodes into one report using Batch process, without long operations. Even fit Pantheon.io execution time limits.

The initial task was to generate the PDF reports from the nodes of the specified content type using the Drupal 8 Entity Print module.
The main use case of the Entity Print module allows to select the number of nodes and print the single PDF.
Entity Print module allows using custom templates etc.

Drupal

This working great for 10 or 20 nodes. But we faced with the Pantheon memory limits and the PHP execution time limits when we tried to generate the PDF from 600 nodes. In our case, this procedure could take in about 2 hours and near 1GB of memory and that was higher then Pantheon limits, but Entity Print module does not support the batch operations.
The main idea is to generate one PDF from the one node and after that to merge all the single PDFs into one big PDF.
Solution was found in batch function that provides the ability to generate 600 single PDFs from 600 nodes.
We also decided to use a drush tool to decrease the memory usage and lowing the execution time to achieve our goals and make this task possible and we have implemented the drush function that used our batch function as well.
This batch function can be run from the Drupal dashboard if needed.

The results were great — 596 posts processed in about 14 minutes without any memory or timeout related issues.
The next step is to merge all single PDFs into one big PDF.
We created another drush function that uses the GhostScript and merges all PDFs into one.

We got the next results — the batch execution time was about 9 minutes and the result is 596 paged PDF (about 25MB).