Report template

Minimal viable template

A minimal template would look like:

<template id="report_invoice">
    <t t-call="report.html_container">
        <t t-foreach="docs" t-as="o">
            <t t-call="report.external_layout">
                <div class="page">
                    <h2>Report title</h2>
                    <p>This object's name is <span t-field="o.name"/></p>
                </div>
            </t>
        </t>
    </t>
</template>

Callingexternal_layoutwill add the default header and footer on your report. The PDF body will be the content inside the<div class="page">. The template'sidmust be the name specified in the report declaration; for exampleaccount.report_invoicefor the above report. Since this is a QWeb template, you can access all the fields of thedocsobjects received by the template.

There are some specific variables accessible in reports, mainly:

docs

records for the current report

doc_ids

list of ids for the docs records

doc_model

model for the docs records

time

a reference to time from the Python standard library

user

res.user record for the user printing the report

res_company

record for the current user 's company

If you wish to access other records/models in the template, you will needa custom report.

Translatable Templates

If you wish to translate reports (to the language of a partner, for example), you need to define two templates:

  • The main report template
  • The translatable document

You can then call the translatable document from your main template with the attributet-langset to a language code (for examplefroren_US) or to a record field. You will also need to re-browse the related records with the proper context if you use fields that are translatable (like country names, sales conditions, etc.)

Warning

If your report template does not use translatable record fields, re-browsing the record in another language is_not_necessary and will impact performances.

For example, let's look at the Sale Order report from the Sale module:

<!-- Main template -->
<template id="report_saleorder">
    <t t-call="report.html_container">
        <t t-foreach="docs" t-as="doc">
            <t t-call="sale.report_saleorder_document" t-lang="doc.partner_id.lang"/>
        </t>
    </t>
</template>

<!-- Translatable template -->
<template id="report_saleorder_document">
    <!-- Re-browse of the record with the partner lang -->
    <t t-set="doc" t-value="doc.with_context({'lang':doc.partner_id.lang})" />
    <t t-call="report.external_layout">
        <div class="page">
            <div class="oe_structure"/>
            <div class="row">
                <div class="col-xs-6">
                    <strong t-if="doc.partner_shipping_id == doc.partner_invoice_id">
                        Invoice and shipping address:</strong>
                    <strong t-if="doc.partner_shipping_id != doc.partner_invoice_id">
                        Invoice address:
                    </strong>
                    <div t-field="doc.partner_invoice_id" t-options="{&quot;no_marker&quot;: True}"/>
                <...>
            <div class="oe_structure"/>
        </div>
    </t>
</template>

The main template calls the translatable template with doc.partner_id.lang as a t-lang parameter, so it will be rendered in the language of the partner. This way, each Sale Order will be printed in the language of the corresponding customer. If you wish to translate only the body of the document, but keep the header and footer in a default language, you could call the report's external layout this way:

<t t-call="report.external_layout" t-lang="en_US">

Tip

Please take note that this works only when calling external templates, you will not be able to translate part of a document by setting at-langattribute on an xml node other thant-call. If you wish to translate part of a template, you can create an external template with this partial template and call it from the main one with thet-langattribute.

Barcodes

Barcodes are images returned by a controller and can easily be embedded in reports thanks to the QWeb syntax:

<img t-att-src="'/report/barcode/QR/%s' % 'My text in qr code'"/>

More parameters can be passed as a query string

<img t-att-src="'/report/barcode/?
    type=%s&value=%s&width=%s&height=%s'%('QR', 'text', 200, 200)"/>

Useful Remarks

  • Twitter Bootstrap and FontAwesome classes can be used in your report template
  • Local CSS can be put directly in the template
  • Global CSS can be inserted in the main report layout by inheriting its template and inserting your CSS:
<template id="report_saleorder_style" inherit_id="report.style">
  <xpath expr=".">
    <t>
      .example-css-class {
        background-color: red;
      }
    </t>
  </xpath>
</template>
  • If it appears that your PDF report is missing the styles, please check these instructions.

results matching ""

    No results matching ""