How to create Qweb based pdf reports
Qweb is a templating engine that converts XML to HTML. Mainly used to define report
templates. Reports are defined based on report actions and report templates. The
report action generates a report based on a Python function or a print button. We
can create custom reports using report actions in Odoo.
Report Actions (ir.actions.report):
The code below is an example of a report action in Odoo 15. Create this XML file
in the module report directory and add this file to the manifest.
<record id="action_report_vehicle_order" model="ir.actions.report">
<field name="name">Vehicle Order</field>
<field name="model">vehicle.report</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">vehicle.report_vehicle_order</field>
<field name="report_file">vehicle.report_vehicle_order</field>
<field name="print_report_name">'Vehicle Report - %s' % (object.name))</field>
<field name="binding_model_id" ref="model_vehcicle_report"/>
<field name="binding_type">report</field>
</record>
The definition of each field in the report operation is described below.
name: Mandatory field used to name the report.
model: Mandatory field, contains the related model where the data taken
from.
report_type: Used to specify types of report qweb-pdf or qweb-html.
report_name: Mandatory field that specifies the external_id of the
report template.
print_report_name: Optional, used to name the generated report file with
a Python expression as desired.
binding_model_id: In some cases, the report needs to be printed from the
view, and binding_model_id needs to be defined. For example, if the model is sale.order,
then the binding_model_id is model_sale_order.
How to Create a Report Template?
Templates are created in HTML format. The layout is written in the <template> tag.
The T-Call property allows you to use multiple templates within a single template.
Corresponding is an example of a sample report template.
<template id="report_vehicle_order">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="web.external_layout">
<div class="page">
<h2>Sample Report</h2>
<p>span t-field="o.name"/></p>
</div>
</t>
</t>
</t>
</template>
In the example, report_vehicle_order is an external ID that needs to be defined in the
report action. t-call=web.external_layout is used to insert Odoo's default web layout
into the report.