Paged media CSS doesn't support more complex page header/footer requirements, like tables. A solution to this is to create a separate page with such a header, and merge this into the PDF on the required pages. A proof of concept was written by pikhovkin in this Gist.
Overlay
: Represents one HTML page to be overlaid on a PDF document.
template_name_or_list
: the name of the HTML template, which is resolved with jinja2's get_or_select_template method.fn_include_on_page
: a function that takes a 0-based page number, and returns a boolean that indicates if it needs to be included on that page.kwargs
: context used to render the overlay. Can also be used to dynamically position content.
render_pdf_with_overlays
renders the PDF like Flask-WeasyPrint's render_pdf
, but with overlays.
-
stylesheets
are also used to render the Overlay, so if any FontConfiguration is included, it is also included in the Overlays. -
the
overlays
templates are rendered to HTML with the supplied context, and this context is extended with thepage
andpages
entries, indicating current 1-based page number and number of pages (CSS page and pages counters don't work with single page overlays)#example: html = flask.render_template('my_report.html') first_page_overlay = Overlay('first_page_overlay.html', fn_include_on_page=first_page, left='10cm', top='5cm') default_page_overlay = Overlay('default_page_overlay.html', fn_include_on_page=not_first_page) pdf_response = render_pdf_with_overlays(HTML(string=html), first_page_overlay, default_page_overlay)