Skip to main content

Webkit based PDF report engine

https://travis-ci.org/openlabs/trytond-report-webkit.png?branch=develop

This package allows you to build HTML based reports and then convert them into PDFs using either wkhtmltopdf which uses the webkit rendering engine and QT. (WebKit is the engine of Apples Safari). or Weasyprint

The templates are written using Jinja 2 template engine. Template Inheritance is also supported to make it easier to extend existing emails and having a single theme for all your emails.

The package also supports Genshi Template Inheritance.

Using this in your projects

Instead of using the default report class from trytond.report use the ReportWebkit class from this package instead.

from openlabs_report_webkit import ReportWebkit

class UserReport(ReportWebkit):
    __name__ = 'res.user'

    @classmethod
    def get_jinja_filters(cls, *args, **kwargs):
        """
        Add my custom filters
        """
        filters = super(UserReport, cls).get_jinja_filters(*args, **kwargs)
        filters.update({
            'nl2br': lambda value: value.replace('\n','<br>\n')
        })
        return filters

Output Formats

To get PDF outputs (instead of standard html) ensure that the report definition in xml clearly shows the extension as PDF. This could be changed from the tryton administration section too.

To convert to PDFs this module needs either the wkhtmltopdf or weasyprint installed.

Installing wkhtmltopdf (default)

Installing wkhtmltopdf from debian repositories usually install an older version which does not have support for features like page numbers at the end of each page. So remember to install the latest one published by the maintainers. For details refer to wkhtmltopdf project website.

Installing weasyprint

Weasyprint is a much lighter option (in comparison to wkhtmltopdf) but with lesser features. Weasyprint can be installed from PYPI.

pip install weasyprint

To use weasyprint, your report implementation class must explicitly override the convert api to use weasyprint. Example:

# TODO: an example here

Template Filters

Tryton HTML reports arrive with some builtin Template filters (in addition to the built-in filters of Jinja2) to make things easier:

dateformat(date, format=’medium’)

Format the date with the current language from the context. For other possible formats, refer the babel documentation.

Example

<td>Date</td>
<td>{{ sale.date|dateformat }}</td>

datetimeformat(datetime, format)

Format the datetime with the current language from the context. For other possible formats, refer the babel documentation.

Example

<td>Created on {{ sale.create_date|datetimeformat('long') }}</td>

currencyformat(amount, currency, format=None)

Return formatted currency value. For more formatting information refer babel documentation

Example

<td>Total Value</td>
<td>{{ sale.total_amount|currencyformat(sale.currency.code) }}</td>

modulepath(name)

Get the absolute Path of a file within a module

Example

<img src="{{ 'company/logo.png'|modulepath }}"/>

Of course you can add your own as stated above.

Including Styles

To include stylesheets, images or any other static data you have two options:

  1. Have Tryton serving your files by adding the static-directory to your Tryton json_path

  2. Bundle your static files inside the reports module and reference using

<link rel="stylesheet" href="{{ 'reports/main.css' | module_path }}" type="text/css">

The second approach comes with the downside that static files will only be available on the server, so you can only see the formatted pdf

Adding as a dependency

You can add the report toolkit as a dependent package of your tryton module by adding into the install_requires list on your setup.py script. Remember to specify the version numbers carefully, or the latest version of the package available would be installed.

For example if your module is for version 2.6 of tryton, the line to add would be

install_requires = [
    ...,
    'openlabs_report_webkit>=2.6,<2.7'
    ...,
]

If you want to use weasyprint instead of whtmltopdf, it might be a good idea to change the above line to

'openlabs_report_webkit[weasyprint]>=2.6,<2.7'

Gotchas!

The report file is missing

  • Did you add the template file to the package_data in your setup.py ?

  • Did you add the template file extension to the included files in MANIFEST ?

Authors and Contributors

This module was built at Openlabs.

We gratefully acknowledge contributions by:

Professional Support

This module is professionally supported by Openlabs. If you are looking for on-site teaching or consulting support, contact our sales and support teams.

Metadata

Release files for openlabs_report_webkit 4.0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openlabs_report_webkit 4.0.1.1
File Size Uploaded
openlabs_report_webkit-4.0.1.1.tar.gz 8.3 kB Details

Release files / openlabs_report_webkit-4.0.1.1.tar.gz

Download URL openlabs_report_webkit-4.0.1.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
26f7d0582768af7dbe2ac23411a659c0255f69f86684fab8b0129622056120bf
BLAKE2b-256 checksum
How to use checksums
2895345287ecc68e080a3edbe9442fc8cebfa8f4b9ecab7bb5e9a90de32d12c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

4.0.1.1 This release

1 release file

4.0.1.0

1 release file

4.0.0.2

1 release file

4.0.0.1

1 release file

3.6.0.2

1 release file

3.4.1.1

1 release file

3.4.1.0

1 release file

3.4.0.4

1 release file

3.4.0.3

1 release file

3.4.0.2

1 release file

3.2.0.4

1 release file

3.2.0.3

1 release file

3.2.0.2

1 release file

3.2.0.1

1 release file

3.0.3.0

1 release file

3.0.2.1

1 release file

3.0.2.0

1 release file

3.0.1.0

1 release file

2.6.2.0

1 release file

2.6.1.0

1 release file

2.6.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page