- πͺπΈSpain uridrupal
This is old, but this is how I did it:
$html = Drupal::service('renderer')->renderRoot($array); /** @var PrintBuilder $print_builder*/ $print_builder = Drupal::service('entity_print.print_builder'); /** @var RendererInterface $renderer*/ $renderer = Drupal::service('entity_print.renderer_factory')->create([$entity]); $print_engine = Drupal::service('plugin.manager.entity_print.print_engine')->createInstance('dompdf'); $print_engine->addPage($html); $content = $renderer->render([$entity]); $render = [ '#theme' => 'your_template', '#title' => $this->t('View @type', ['@type' => $print_engine->getExportType()->label()]), '#content' => $content, '#attached' => ['your_module/library'], ]; $print_engine->addPage($renderer->generateHtml([$entity], $render, FALSE, TRUE)); return (new StreamedResponse(function () use ($print_builder, $entity, $print_engine) { // The printed document is sent straight to the browser. $print_builder->deliverPrintable([$event_notes], $print_engine, FALSE, TRUE); }))->send();
As far as I understood, the module requires an entity to work, you can't make it work only with a render array. So I did setup in a random entity a "PDF" view mode (which I knew I wouldn't need to export to PDF later), created the template twig. That template Twig shows nothing, it's just there to make the genereateHtml() method work.
$array is the result of
return [ '#theme' => 'your_template', '#var1' => $var1, '#var2' => $var2, '#var3' => $var3 ];
Hope it helps somebody else.