2

I´m trying to use the entity_print module for generating a PDF in Drupal 8. I want to print out HTML. My module has no entities it gets data from a solr server and generates HTML output.

My first thing is to make a route and a sort of flag to fire the pdf generation:

mymodule.page_detail:
path: '/detail/{Title}/{ObjIdentifier}'
defaults:
_controller: '\Drupal\mymodule\Controller\myController::detailpage'
Title: ''
ObjIdentifier: ''
ViewPdf: '0'
...

mymodule.page_detail_pdf:
path: '/detail/pdf/{Title}/{ObjIdentifier}'
defaults:
_controller: '\Drupal\mymodule\Controller\myController::detailpage'
Title: ''
ObjIdentifier: ''
ViewPdf: '1'

...

Then in my Controller I check for

if (isset($myModuleViewPdf) && $myModuleViewPdf == "1") {
  ...

Now the entity Print magic should come into play. I use the services and make a print engine and a print builder:

$print_engine = \Drupal::service('plugin.manager.entity_print.print_engine')->createSelectedInstance('pdf');
$print_builder = \Drupal::service('entity_print.print_builder');

Now I´m struggeling somehow. When I look over the functions docroot/modules/entity_print/src/PrintBuilder.php

I see that they are using entities for the output but I want to use HTML. How can I do this?

Greetings Lars

5

If you want to send the PDF directly to the user then you can do something like this:

// Get your HTML from Solr.
$html = $this->getHtmlFromSolr();

$print_engine = \Drupal::service('plugin.manager.entity_print.print_engine')->createSelectedInstance('pdf');

$print_engine->addPage($html);
$print_engine->send('mydocument.pdf');
| improve this answer | |
  • I used this in my controller and it´s working. But can I use this also at a place where I can use the twig template? in the mymodule.module or in a process function? The reason is, we seperate code from design and the Frontend-Developer should be able to handle the twig Templates. When i now begin managing output via PHP I will mess things up. – Lars Jendrzejewski Mar 2 '17 at 10:52
  • Well you said your data was coming from Solr, you're only using Entity Print to convert HTML to PDF, whether you can use twig templates for your frontend developer would depend on whatever process you use to get your data into HTML. – benjy Mar 3 '17 at 9:01
  • Worked for me. Saved my day. Keep posting such useful answers. Best of luck. – Pupil Nov 28 '17 at 7:36
  • This has saved my day also!! with so many other developer-chaos trying to destroy/frustrate my day, this is really really useful!!! THANK YOU!! THANK YOU!! – Raf A. Dec 17 '17 at 9:50
0

The previos code works the first time you load the page but will throw errors in your watchdog, and you will give a WSOD when you reload the page.

You need a new Strem Response for the PDF, you cannot reuse the controller stream.

$html = $this->getHtmlFromSolr();

$print_engine = \Drupal::service('plugin.manager.entity_print.print_engine')->createSelectedInstance('pdf');
$print_engine->addPage($html);

return (new StreamedResponse(function () use ($print_engine) {
  $print_engine->send('youdocument.pdf');
}))->send();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.