Problem/Motivation

There's currently a PRE_SEND event which allows modifications to the PDF after previous HTML has been rendered. If we use this event to add a cover page, the cover page appears at the end of the PDF due to the way the PHP library phpwkhtmltopdf adds HTML sequentially.

Adding a PRE_RENDER event would allow for adding a cover page at the beginning (and anything else which might make sense there).

Example event subscriber method using phpwkhtmltopdf:

public function preRender($event) {
  $printEngine = $event->getPrintEngine();
  $printObject = $printEngine->getPrintObject();

  // Grab HTML as a string from somewhere.
  $coverHtml = $this->generateCoverPage();

  // Set cover page.
  $printObject->addCover($coverHtml);
}
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

imclean created an issue. See original summary.

imclean’s picture

Status: Active » Needs review
StatusFileSize
new3.6 KB
imclean’s picture

Issue summary: View changes
imclean’s picture

Issue summary: View changes

imclean’s picture

Reference: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php#L14...

    public function addCover($input, $options = array(), $type = null)
    {
        $options['input'] = ($this->version9 ? '--' : '') . 'cover';
        $options['inputArg'] = $this->ensureUrlOrFile($input, $type);
        $this->_objects[] = $this->ensureUrlOrFileOptions($options);
        return $this;
    }
alexandersluiter’s picture

Would this not be a "PRE_DELIVER" and "PRE_SAVE" event instead? PRE_RENDER would be right before the actual HTML rendering.

See https://www.drupal.org/project/entity_print/issues/2860122#comment-14423265

alexandersluiter’s picture

imclean’s picture

@alexandersluiter #7, that's probably more accurate. Although I'm not sure PrintBuilder->deliverPrintable() is the best place to dispatch these events as they don't get fired with the debug route so the preview won't always match the PDF generated.

Maybe deliverPrintable() could be broken up a bit. It only really needs to get the filename and send the PDF to the browser.

There's a related issue which would make viewing the printer friendly page easier.

drubb’s picture

Status: Needs review » Needs work
StatusFileSize
new3.71 KB

Tested this patch with the mPDF engine, works great for e.g. adding headers and footers. Example:

public function onEntityPrintPreRender(PreRenderPrintEvent $event): void {
    $printEngine = $event->getPrintEngine();
    if (!$printEngine || $printEngine->getPluginId() !== 'mpdf') {
      return;
    }
    /** @var \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\Mpdf $printEngine */
    $printEngine->getPrintObject()->SetHTMLHeader('This is the header text...');
    $printEngine->getPrintObject()->SetHTMLFooter('This is the footer text...');
  }

In many engines, headers and footers must be added before rendering anything, so that's a perfect use case IMHO.

Small fix:

The code used for dispatching the event in PrintBuilder.php is broken (wrong argument order). I've therefore attached a modified version of the patch from MR-13.

internetter made their first commit to this issue’s fork.

internetter’s picture

Hi,

just updated the MR13 to refer to the last version of entity_print and included the changes from #10.

Additionally I added a possibility to change the Entities from within the EventSubscriber. So I can change the entities inside a book node to always use the complete book nodes for rendering. So I get a complete PDF of the entire book, not only the entity which the user has clicked on. I also add a cover and table-of-contents page to the pdf before rendering the complete content of the book.

bye Martin