Skip to content

Template Rendering

WeasyPrint Templates

We use the powerful WeasyPrint PDF generation engine to create custom reports and labels.

WeasyPrint

WeasyPrint is an extremely powerful and flexible reporting library. Refer to the WeasyPrint docs for further information.

Stylesheets

Templates are rendered using standard HTML / CSS - if you are familiar with web page layout, you're ready to go!

Template Language

Uploaded report template files are passed through the django template rendering framework, and as such accept the same variable template strings as any other django template file. Different variables are passed to the report template (based on the context of the report) and can be used to customize the contents of the generated PDF.

Context Variables

Context Variables

Templates will have different variables available to them depending on the report type. Read the detailed information on each available report type for further information.

Please refer to the Context variables page.

Conditional Rendering

The django template system allows for conditional rendering, providing conditional flow statements such as:

{% if <condition> %}
{% do_something %}
{% elif <other_condition> %}
<!-- something else -->
{% else %}
<!-- finally -->
{% endif %}
{% for <item> in <list> %}
Item: {{ item }}
{% endfor %}

Conditionals

Refer to the django template language documentation for more information.

Localization Issues

Depending on your localization scheme, inputting raw numbers into the formatting section template can cause some unintended issues. Consider the block below which specifies the page size for a rendered template:

<head>
    <style>
        @page {
            size: {{ width }}mm {{ height }}mm;
            margin: 0mm;
        }
    </style>
</head>

If localization settings on the InvenTree server use a comma (,) character as a decimal separator, this may produce an output like:

<head>
    <style>
        @page {
            size: 57,3mm 99,0mm;
            margin: 0mm;
        }
    </style>
</head>

The resulting <style> CSS block will be invalid!

So, if you are writing a template which has custom formatting, (or any other sections which cannot handle comma decimal separators) you must wrap that section in a {% localize off %} block:

{% load l10n %}
<head>
    <style>
        @page {
            {% localize off %}
            size: {{ width }}mm {{ height }}mm;
            {% endlocalize %}
            margin: 0mm;
        }
    </style>
</head>

Close it out

Don't forget to end with a {% endlocalize %} tag!

l10n

You will need to add {% load l10n %} to the top of your template file to use the {% localize %} tag.