Create a custom print format with HTML and Jinja
A custom print format gives you full control of the printed layout. You write it in HTML, and you insert document values with Jinja tags. ERPNext renders the template on the server.
Use a custom format when the Print Format Builder cannot produce the layout that you need.
Prerequisites
- The System Manager role.
- Basic knowledge of HTML and CSS.
- The field names of the fields that you want to print. To find a field name, open Customize Form, select the DocType, and open the field row. The Name column shows the field name, for example
customer_name.
Create the print format
- Open the Print Format list, and select New.
- Enter a name.
- In DocType, select the document type, for example Sales Invoice. ERPNext fills in Module.
- Select Custom Format.
- In Print Format Type, keep Jinja.
- Enter your template in HTML.
- Select Save.
To use the new format, open the print view of a document and select the format in the print format dropdown.
If you do not select Custom Format, the Style Settings section appears instead. There you can align labels to the right, show section headings, show line breaks after sections, and set a Default Print Language and a Google Font.
Print document fields
Every template has access to doc, the document that you print. Write a field as doc.<field name>.
<h3>{{ doc.select_print_heading or _("Invoice") }}</h3>
<p>{{ _("Customer") }}: {{ doc.customer_name }}</p>
<p>{{ _("Date") }}: {{ doc.get_formatted("posting_date") }}</p>
<p>{{ _("Grand Total") }}: {{ doc.get_formatted("grand_total") }}</p>- Use
doc.get_formatted("<field name>")to print dates and currency amounts in the user's format. - Wrap fixed text in
_()so that ERPNext translates it, for example{{ _("Customer") }}.
Print the rows of a child table
A child table, such as the Items table, holds many rows. Loop over the rows with for. The field name of the table in Sales Invoice is items.
As a table
<table class="table table-bordered">
<thead>
<tr>
<th>{{ _("Sr") }}</th>
<th>{{ _("Item") }}</th>
<th class="text-right">{{ _("Qty") }}</th>
<th class="text-right">{{ _("Rate") }}</th>
<th class="text-right">{{ _("Amount") }}</th>
</tr>
</thead>
<tbody>
{%- for row in doc.items -%}
<tr>
<td>{{ row.idx }}</td>
<td>
{{ row.item_name }}
{%- if row.item_code != row.item_name -%}
<br>{{ _("Item Code") }}: {{ row.item_code }}
{%- endif -%}
</td>
<td class="text-right">{{ row.qty }} {{ row.uom or row.stock_uom }}</td>
<td class="text-right">{{ row.get_formatted("rate", doc) }}</td>
<td class="text-right">{{ row.get_formatted("amount", doc) }}</td>
</tr>
{%- endfor -%}
</tbody>
</table>As a list
<ul>
{% for row in doc.items %}
<li>
{{ row.get_formatted("item_code", doc) }}:
{{ row.get_formatted("qty", doc) }} x {{ row.get_formatted("rate", doc) }}
= {{ row.get_formatted("amount", doc) }}
</li>
{% endfor %}
</ul>For a row field, pass the parent document to get_formatted, as in row.get_formatted("rate", doc). ERPNext then uses the currency of the document.
Style the print format
Print formats use the Bootstrap CSS framework, so you can use Bootstrap classes such as row, col-xs-6, table, and text-right. Add your own CSS in the Custom CSS field.
Share a print format as standard
If developer mode is on for your site, set Standard to Yes to save the print format as a file in an app. Use this only when you develop an app.