weasyprint: Printing documents using WeasyPrint

The lino.modlib.weasyprint plugin installs two build methods for generating printable documents using weasyprint.

Applications that use this plugin will have 'weasyprint' in their install_requires.

The two build methods defined by this plugin both use the same input template, whose ending must be .weasy.html. Both methods then render the input template through Jinja with the standard context variables (defined by get_printable_context. The base build method WeasyBuildMethod then returns this HTML output "as is", the other method runs weasyprint over the HTML file to convert it to a .pdf file.

Examples in this document use the lino_book.projects.lydia demo project.

>>> from lino import startup
>>> startup('lino_book.projects.lydia.settings.doctests')
>>> from lino.api.shell import *
>>> from lino.api.doctest import *

See also printing : Basic printing functionality.

Build methods

This plugin defines no models, it just adds two build methods to your the global list of build methods ():class:lino.modlib.printing.BuildMethods).

class lino.modlib.weasyprint.WeasyBuildMethod

The base class for both build methods.

class lino.modlib.weasyprint.WeasyHtmlBuildMethod

Renders the input template and returns the unmodified output as plain HTML.

class lino.modlib.weasyprint.WeasyPdfBuildMethod

Like WeasyBuildMethod, but the rendered HTML is then passed through weasyprint which converts from HTML to PDF.

Templates

weasyprint/base.weasy.html

Defines the general HTML page to be included by every template. This is meant to be included by the actual templates.

Source code: https://github.com/lino-framework/lino/blob/master/lino/modlib/weasyprint/config/weasyprint/base.weasy.html

You can automagically add a logo to all your weasyprint documents by adding a local config directory with a subdirectory weasyprint containing a file named logo.jpg. The logo will get printed in the top left area of every page (unless you change the template).

The template defines the following blocks:

  • pagesize

  • bottomright

  • bottomleft

  • header

  • intro

  • main

Weasyprint templates defined by other plugins

Warnings about Cairo and Pango

This plugin installs a warnings filter for the cffi.model module in order to get rid of a disturbing warning There are known rendering problems with Cairo <= 1.14.0 and @font-face support needs Pango >= 1.38 issued by weasyprint.