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);
}
Comments
Comment #2
imclean commentedComment #3
imclean commentedComment #4
imclean commentedComment #6
imclean commentedReference: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php#L14...
Comment #7
alexandersluiter commentedWould 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
Comment #8
alexandersluiter commentedComment #9
imclean commented@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.
Comment #10
drubbTested this patch with the mPDF engine, works great for e.g. adding headers and footers. Example:
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.
Comment #12
internetter commentedHi,
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