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:
- create a HTML layout format that is fed all the fields from the views page
- rightly set the footer/header variable for output in the entity-print.html template mentioned above
- 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.
{{ kint(footer) }}
to your template?kint
sub-module enabled? if not, please be sure to download devel and then enablekint
. Here is a tutorial: webwash.net/how-to-print-variables-using-kint-in-drupal-8{{ kint() }}
and then{{ kint(footer) }}
, so now when I tried to download the pdf it took a while and thenFatal error: Allowed memory size of 134217728 bytes exhausted
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 afooter
key, then it seems your preprocess and your twig aren't talking.