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.



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

  • lino_xl.lib.bevats -- bevats/Declaration/default.weasy.html

    In demo project lydia, go to Accounting ‣ VAT Declarations and print one of them.

  • lino_xl.lib.courses -- courses/Course/presence_sheet.weasy.html

    In demo project roger, open the detail view of some course and click on one of the Presence sheet links.

  • lino_xl.lib.ledger -- contacts/Partner/payment_reminder.weasy.html

  • lino_xl.lib.lists -- lists/List/list_members.weasy.html

  • lino_xl.lib.sheets -- sheets/Report/default.weasy.html

  • lino_xl.lib.working -- working/ServiceReport/default.weasy.html

Here is a list of the weasy templates included with the Lino Extensions Library:

>>> import lino_xl
>>> from os.path import dirname
>>> from atelier.sheller import Sheller
>>> shell = Sheller(dirname(lino_xl.__file__))
>>> shell("find -name '*.weasy.html' | sort")

Note also that lino_xl.lib.excerpts contains the excerpts/base.weasy.html template, which inherits from weasyprint/base.weasy.html.

For other usage examples see the specs of Lino Welfare.

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.