How to create Qweb-based pdf reports
Odoo plays a vital role in making a business efficient. The analysis of various areas is important in terms of the organization's growth in the ERP system. This is where we use reporting.
Qweb is a reporting engine that generates PDF reports. It handles data easily using XML. The Qweb template engine usually converts XLM to HTML. And PDF rendering is done by wkhtmltopdf. Reports are generated using report action and report templates for the action to use.
Report Action:
Report actions are triggered when reports need to be generated. The report action is written inside the XML file created in the report file of the module. The report action can be written like this;
<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 fields of the report action are mentioned below in detail;
● name: Name of the report to be specified.
● model : It’s a mandatory field from which the records will be retrieved into the report.
● report_type: It specifies the type of the report, such as ‘qweb-text,’ ‘qweb-html,’ ‘qweb-pdf’. This used mainly two values qweb-pdf and qweb-html. The default value is qweb-pdf.
● report_name: Here, it specifies the external id of the report template.
● print_report_name: It specifies the report file name, and here the python expressions also can be used.
● binding_model_id: If the reports need to be printed from the Print action, then the required model id can be specified here.
Report Templates:
The structure of the report species in the report templates. It’s an XML file created inside the report file of the module. Inside the template, HTML is used to design the report as required.
<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>
The id of the template is external if that is mentioned in the report action. By calling the external_layout, the report will get the default layout of Odoo. The template always contains the variables like time, user, res_company, website, web_base_url, and context_timestamp.