1

My requirement is to convert a views page into a PDF. I have created the views page with table formatting and a PDF plugin attached to the header that allows one to download the views page as PDF. I created an entity-print.html twig template and an HTML structure copied from the answer in this post How to customize entity print pdf and created a twig variable 'footer' in THEME_preprocess_page function in the theme's .theme file but the calls {{ footer }} to that variable in the template returns nothing.

In other to at least test CSS formatting, I placed these in the respective files:

ENTITY-PRINT.HTML.TWIG

<html>
<head>
    <meta charset="utf-8">
    <title>{{ title }}</title>
    {{ entity_print_css }}
</head>
<body>
{# The footer is repeated on all pages and must be printed first. #}

<footer>
    {{  footer  }}
</footer>
{%  if cover_page  %}
<div class="cover-page">
    {{ cover_page }}
</div>
{%  endif  %}
{# The header is repeated after the cover page for all pages. #}
<header>
    {{  header  }}
</header>
<div class="page">
    {{ content }}
</div>
</body>
</html>

THEMENAME.THEME

function THEMENAME_preprocess_page(&$variables) {
  $variables['footer'] = "HERE IS YOUR FOOTER";
  $variables['header'] = "HERE IS YOUR HEADER";
}

IN .INFO.YML

entity_print:
 views:
   views_page_machine_name: 'theme_name/result-pdf'

IN .LIBRARIES.YML

result-pdf:
  css:
    theme:
      styles/result-pdf.css: {}

IN CSS FILE (i.e. result-pdf.css)

* {
    color: red;
}

div {
    background: blue;
}

and then created result-pdf.css but the styles did not change anything in the generated pdf.

One of the answers in another post Entity print get variable in twig template pointed to creating an entity specific template for layout of the PDF with HTML as required. For example, for the article content type you have node--article--pdf.html.twig. What is the template suggestion for a views as described in my case?

The question is how do I achieve:

  1. create a HTML layout format that is fed all the fields from the views page
  2. rightly set the footer/header variable for output in the entity-print.html template mentioned above
  3. turn bullet into A pdf (so that the download link on the views page generates the formatted html as pdf).

Perhaps bullet 1-3 is irrelevant, a step by step direction on how to have a views page formatted as required and converted to PDF using the entity print module will be appreciated.

13
  • What do you see when you add {{ kint(footer) }} to your template? – Beau Dec 23 '20 at 18:39
  • @Beau Without it the download link works but with it and clearing cache I got this error " Error message Error generating document: Failed to generate PDF: 'box_sizing' is not a recognized CSS property., 'overflow_x' is not a recognized.., '.kint ul:not(.kint-tabs) li' is not valid, The CSS selector '.kint ul:not(.kint-tabs) li>dl' is not valid, The CSS selector '.kint ul.kint-tabs li:not(.kint-active-tab)' is not valid, The CSS selector '.kint>dl:not(.kint-trace)>dd>ul.kint-tabs li' is not valid, The CSS selector '.kint>dl:not(.kint-trace)>dd>ul.kint-tabs li.kint-active-tab' is not valid " – Ojchris Dec 25 '20 at 16:55
  • I suppose I should have also asked - do you have the kint sub-module enabled? if not, please be sure to download devel and then enable kint. Here is a tutorial: webwash.net/how-to-print-variables-using-kint-in-drupal-8 – Beau Dec 25 '20 at 22:35
  • I've got devel installed with kint enabled already. Maybe the issue is what template to place it in. I'm placing it it in entity-print.html.twig and added the kint call {{ kint() }} and then {{ kint(footer) }}, so now when I tried to download the pdf it took a while and then Fatal error: Allowed memory size of 134217728 bytes exhausted – Ojchris Dec 27 '20 at 16:12
  • kint() is a pretty resource-heavy function. Try eliminating the {{ kint() }} call. If you continue to receive memory exhaust errors, then try {{ kint(footer|keys) }} in place of {{ kint(footer) }}. Also, use {{ kint(_context|keys) }} to verify what top level keys are actually available to you. If you don't see a footer key, then it seems your preprocess and your twig aren't talking. – Beau Dec 27 '20 at 22:49
1

The solution came down to replacing "views" with "view" - without the s and "views_page_machine_name" with "all", such that this:

entity_print:
 views:
   views_page_machine_name : 'theme_name/result-pdf'

became:

entity_print:
 view:
   all: 'theme_name/result-pdf'

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.