{"id":10183,"date":"2026-11-24T15:57:23","date_gmt":"2026-11-24T12:57:23","guid":{"rendered":"https:\/\/machinser.com\/?p=10183"},"modified":"2026-11-24T15:57:23","modified_gmt":"2026-11-24T12:57:23","slug":"odoo-20-qweb-templates-reports","status":"publish","type":"post","link":"https:\/\/machinser.com\/ar\/odoo-20-qweb-templates-reports\/","title":{"rendered":"Inside Odoo 20 Reporting: Master QWeb Templates from XML to PDF"},"content":{"rendered":"<p data-pm-slice=\"1 3 []\">Reports are an essential part of everyday business operations. Businesses use them to generate invoices, quotations, sales orders, delivery documents, purchase orders, employee records, and many other documents.<\/p>\n<p>Odoo provides a flexible reporting system that allows developers to generate these documents using information already stored in the database. One of the main technologies behind this functionality is QWeb, Odoo&#8217;s XML-based templating engine.<\/p>\n<p>QWeb allows developers to define the structure and appearance of documents, display dynamic information from Odoo records, and control how data is presented. By combining QWeb templates with report actions, developers can create custom HTML and PDF reports that users can access directly from the Odoo interface.<\/p>\n<p>In this guide, we will explore how QWeb templates work in Odoo 20, understand the components of its reporting system, and build a custom PDF report step by step.<\/p>\n<h2>1. What Is QWeb in Odoo 20?<\/h2>\n<p>QWeb is Odoo&#8217;s XML-based templating engine. It is used to generate dynamic HTML content by combining XML structures with data and template directives.<\/p>\n<p>Instead of manually creating a separate document for every business record, developers can create one template and allow Odoo to populate it with information from the database.<\/p>\n<p>For example, consider a sales order containing:<\/p>\n<ul data-spread=\"false\">\n<li>Customer name<\/li>\n<li>Order reference<\/li>\n<li>Order date<\/li>\n<li>Products<\/li>\n<li>Quantities<\/li>\n<li>Unit prices<\/li>\n<li>Subtotals<\/li>\n<li>Total amount<\/li>\n<\/ul>\n<p>A QWeb template defines where these details should appear. When the report is generated, Odoo retrieves the relevant sales order information and inserts it into the template.<\/p>\n<p>This makes QWeb useful for generating consistent, data-driven documents across different business operations.<\/p>\n<h3>Where Is QWeb Used?<\/h3>\n<p>QWeb is used in several areas of Odoo, including:<\/p>\n<ul data-spread=\"false\">\n<li>PDF reports such as invoices and quotations.<\/li>\n<li>HTML reports and document previews.<\/li>\n<li>Website templates.<\/li>\n<li>Certain interface components and views.<\/li>\n<li>Custom business documents developed through Odoo modules.<\/li>\n<\/ul>\n<p>This article focuses primarily on QWeb&#8217;s use in PDF and HTML reporting.<\/p>\n<h2>2. Understanding the Odoo Reporting Architecture<\/h2>\n<p>A QWeb report is not just an XML file. It involves several components working together.<\/p>\n<h3>2.1 Business Model<\/h3>\n<p>The model contains the business records that the report will display.<\/p>\n<p>For example, a sales order report uses the <code dir=\"ltr\">sale.order<\/code> model.<\/p>\n<h3>2.2 Report Action<\/h3>\n<p>The report action connects the report to an Odoo model and defines how it should be generated. It specifies the report type and identifies the QWeb template to use.<\/p>\n<h3>2.3 QWeb Template<\/h3>\n<p>The template defines the document&#8217;s structure, layout, styling, and dynamic content.<\/p>\n<h3>2.4 Report Rendering Engine<\/h3>\n<p>Odoo processes the template and its associated data to generate the final document.<\/p>\n<p>For PDF reports, Odoo uses <code dir=\"ltr\">wkhtmltopdf<\/code> to convert the generated HTML into a PDF document.<\/p>\n<h3>How the Components Work Together<\/h3>\n<p>The general reporting process is:<\/p>\n<ol start=\"1\" data-spread=\"false\">\n<li>A user opens a business record, such as a sales order.<\/li>\n<li>The user selects a report from the Print menu.<\/li>\n<li>Odoo identifies the associated report action.<\/li>\n<li>The report action identifies the model and QWeb template.<\/li>\n<li>Odoo supplies the relevant record data to the template.<\/li>\n<li>QWeb generates the HTML document.<\/li>\n<li>For a PDF report, Odoo renders the HTML using <code dir=\"ltr\">wkhtmltopdf<\/code>.<\/li>\n<li>The generated document is displayed or downloaded.<\/li>\n<\/ol>\n<p>This separation makes the reporting system flexible. Developers can modify document layouts without changing the underlying business model.<\/p>\n<h2>3. Understanding QWeb Template Directives<\/h2>\n<p>QWeb templates use XML elements and special attributes called directives. These directives generally begin with the prefix <code dir=\"ltr\">t-<\/code>.<\/p>\n<p>They allow developers to display values, apply conditions, repeat elements, define variables, and reuse other templates.<\/p>\n<p>Let&#8217;s explore some commonly used directives.<\/p>\n<h3>3.1 <code dir=\"ltr\">t-out<\/code> \u2013 Displaying Dynamic Values<\/h3>\n<p>The <code dir=\"ltr\">t-out<\/code> directive displays the result of an expression.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-xml\">&lt;p&gt;\r\n    Customer:\r\n    &lt;t t-out=\"o.partner_id.name\"\/&gt;\r\n&lt;\/p&gt;<\/code><\/pre>\n<p>Here, <code dir=\"ltr\">o<\/code> represents the current business record, and <code dir=\"ltr\">partner_id.name<\/code> retrieves the customer&#8217;s name.<\/p>\n<p>QWeb automatically escapes the output, helping prevent unsafe HTML from being inserted into the document.<\/p>\n<h3>3.2 <code dir=\"ltr\">t-field<\/code> \u2013 Displaying Odoo Record Fields<\/h3>\n<p>The <code dir=\"ltr\">t-field<\/code> directive is used to display fields from Odoo records.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-xml\">&lt;span t-field=\"o.date_order\"\/&gt;<\/code><\/pre>\n<p>It is particularly useful for displaying Odoo fields because it supports field-aware formatting.<\/p>\n<p>For example, monetary, date, and relational fields can be formatted according to their field types and relevant widget options.<\/p>\n<h3>3.3 <code dir=\"ltr\">t-if<\/code> and <code dir=\"ltr\">t-else<\/code> \u2013 Conditional Display<\/h3>\n<p>Sometimes a report should display information only when a particular condition is satisfied.<\/p>\n<p>For example, a sales order may contain discounted product lines. Since the discount is stored on the order line, the condition should be checked against <code dir=\"ltr\">line.discount<\/code>.<\/p>\n<pre><code class=\"language-xml\">&lt;t t-foreach=\"o.order_line\" t-as=\"line\"&gt;\r\n    &lt;t t-if=\"line.discount\"&gt;\r\n        &lt;span t-out=\"line.discount\"\/&gt;%\r\n    &lt;\/t&gt;\r\n    &lt;t t-else=\"\"&gt;\r\n        &lt;span&gt;No Discount&lt;\/span&gt;\r\n    &lt;\/t&gt;\r\n&lt;\/t&gt;<\/code><\/pre>\n<p>In this example:<\/p>\n<ul data-spread=\"false\">\n<li><code dir=\"ltr\">t-if<\/code> checks whether the order line has a discount.<\/li>\n<li>If a discount exists, its percentage is displayed.<\/li>\n<li><code dir=\"ltr\">t-else<\/code> displays alternative text when there is no discount.<\/li>\n<\/ul>\n<p>Conditional directives help developers control what appears in the report based on actual business data.<\/p>\n<h3>3.4 <code dir=\"ltr\">t-foreach<\/code> and <code dir=\"ltr\">t-as<\/code> \u2013 Repeating Records<\/h3>\n<p>The <code dir=\"ltr\">t-foreach<\/code> directive is used to iterate over a collection of records. The <code dir=\"ltr\">t-as<\/code> attribute defines the variable used for each item.<\/p>\n<p>For example, a sales order can contain multiple product lines.<\/p>\n<pre><code class=\"language-xml\">&lt;t t-foreach=\"o.order_line\" t-as=\"line\"&gt;\r\n    &lt;tr&gt;\r\n        &lt;td&gt;\r\n            &lt;span t-field=\"line.product_id\"\/&gt;\r\n        &lt;\/td&gt;\r\n        &lt;td&gt;\r\n            &lt;span t-field=\"line.product_uom_qty\"\/&gt;\r\n        &lt;\/td&gt;\r\n        &lt;td&gt;\r\n            &lt;span t-field=\"line.price_unit\"\/&gt;\r\n        &lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n&lt;\/t&gt;<\/code><\/pre>\n<p>Odoo processes each order line and generates a separate table row.<\/p>\n<h3>3.5 <code dir=\"ltr\">t-set<\/code> \u2013 Creating Temporary Variables<\/h3>\n<p>The <code dir=\"ltr\">t-set<\/code> directive creates a variable that can be used within a template.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-xml\">&lt;t t-set=\"total_quantity\"\r\n   t-value=\"sum(o.order_line.mapped('product_uom_qty'))\"\/&gt;<\/code><\/pre>\n<p>The variable can then be displayed:<\/p>\n<pre><code class=\"language-xml\">&lt;p&gt;\r\n    Total Quantity:\r\n    &lt;t t-out=\"total_quantity\"\/&gt;\r\n&lt;\/p&gt;<\/code><\/pre>\n<p>This is useful when a report needs calculated or intermediate values.<\/p>\n<h3>3.6 <code dir=\"ltr\">t-call<\/code> \u2013 Reusing Another Template<\/h3>\n<p>The <code dir=\"ltr\">t-call<\/code> directive allows one template to include another template.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-xml\">&lt;t t-call=\"web.external_layout\"&gt;\r\n    &lt;div class=\"page\"&gt;\r\n        &lt;h2&gt;Sales Order Report&lt;\/h2&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/t&gt;<\/code><\/pre>\n<p>Here, <code dir=\"ltr\">web.external_layout<\/code> provides the standard external report layout, including company header and footer elements.<\/p>\n<p>Template reuse helps developers maintain consistent layouts and avoid duplicating XML code.<\/p>\n<h3>A Note About Older QWeb Directives<\/h3>\n<p>Developers may encounter older QWeb examples using <code dir=\"ltr\">t-esc<\/code> and <code dir=\"ltr\">t-raw<\/code>.<\/p>\n<p>In current QWeb development, <code dir=\"ltr\">t-out<\/code> is the preferred directive for output. It replaces the older output directives in modern templates.<\/p>\n<p>When migrating older custom reports, review these directives and ensure that the output is handled safely. Avoid rendering untrusted HTML content without appropriate sanitization.<\/p>\n<h2>4. Standard Report Layouts in Odoo<\/h2>\n<p>Odoo provides standard layouts that developers can reuse when creating custom reports.<\/p>\n<p>Two important templates are:<\/p>\n<h3><code dir=\"ltr\">web.html_container<\/code><\/h3>\n<p>This template provides the outer HTML structure required for a report.<\/p>\n<p>It is commonly used as the main container around report documents.<\/p>\n<h3><code dir=\"ltr\">web.external_layout<\/code><\/h3>\n<p>This template provides the standard external document layout, including elements such as:<\/p>\n<ul data-spread=\"false\">\n<li>Company logo and details.<\/li>\n<li>Document header.<\/li>\n<li>Footer information.<\/li>\n<li>Page numbering and layout elements.<\/li>\n<\/ul>\n<p>A typical report template uses both:<\/p>\n<pre><code class=\"language-xml\">&lt;template id=\"report_example\"&gt;\r\n    &lt;t t-call=\"web.html_container\"&gt;\r\n        &lt;t t-foreach=\"docs\" t-as=\"o\"&gt;\r\n            &lt;t t-call=\"web.external_layout\"&gt;\r\n                &lt;div class=\"page\"&gt;\r\n                    &lt;h2&gt;Example Report&lt;\/h2&gt;\r\n                    &lt;p&gt;\r\n                        Document:\r\n                        &lt;span t-field=\"o.name\"\/&gt;\r\n                    &lt;\/p&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/t&gt;\r\n        &lt;\/t&gt;\r\n    &lt;\/t&gt;\r\n&lt;\/template&gt;<\/code><\/pre>\n<p>The <code dir=\"ltr\">docs<\/code> variable contains the records being printed. The variable <code dir=\"ltr\">o<\/code> represents the current record inside the loop.<\/p>\n<p>The <code dir=\"ltr\">&lt;div class=\"page\"&gt;<\/code> element contains the actual body of the report.<\/p>\n<h2>5. What Is a Report Action in Odoo?<\/h2>\n<p>A QWeb template defines how a report looks, but Odoo also needs to know when and how to use it.<\/p>\n<p>This is handled through a <strong>Report Action<\/strong>, represented by the model:<\/p>\n<p><code dir=\"ltr\">ir.actions.report<\/code><\/p>\n<p>A report action connects a business model with a QWeb template and defines the report&#8217;s behaviour.<\/p>\n<p>Some important fields include:<\/p>\n<table>\n<tbody>\n<tr>\n<th>Field<\/th>\n<th>Purpose<\/th>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">name<\/code><\/td>\n<td>Name of the report action<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">model<\/code><\/td>\n<td>Business model associated with the report<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">report_type<\/code><\/td>\n<td>Output format, such as PDF or HTML<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">report_name<\/code><\/td>\n<td>External ID of the QWeb template<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">binding_model_id<\/code><\/td>\n<td>Model whose views expose the report in the Print menu<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">print_report_name<\/code><\/td>\n<td>Expression used to define the generated filename<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">groups_id<\/code><\/td>\n<td>Groups permitted to access the report<\/td>\n<\/tr>\n<tr>\n<td><code dir=\"ltr\">paperformat_id<\/code><\/td>\n<td>Paper format used for the generated PDF<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The most commonly used report types are:<\/p>\n<ul data-spread=\"false\">\n<li><code dir=\"ltr\">qweb-pdf<\/code> \u2013 Generates a PDF report.<\/li>\n<li><code dir=\"ltr\">qweb-html<\/code> \u2013 Generates an HTML report.<\/li>\n<\/ul>\n<p>A report action is the connection between the user&#8217;s print request and the QWeb template that produces the document.<\/p>\n<h2>6. Creating a Custom PDF Report in Odoo 20<\/h2>\n<p>Let&#8217;s create a simple custom PDF report for sales orders.<\/p>\n<p>The objective is to generate a document containing the sales order reference, customer details, order date, product information, quantities, unit prices, and total amount.<\/p>\n<p>This example demonstrates a basic custom report implementation.<\/p>\n<h3>Step 1: Create a Custom Module Structure<\/h3>\n<p>Create a custom module named <code dir=\"ltr\">custom_sale_report<\/code>.<\/p>\n<p>Use the following structure:<\/p>\n<pre><code class=\"language-text\">custom_sale_report\/\r\n\u2502\r\n\u251c\u2500\u2500 __init__.py\r\n\u251c\u2500\u2500 __manifest__.py\r\n\u2502\r\n\u2514\u2500\u2500 report\/\r\n    \u2514\u2500\u2500 sale_order_report.xml<\/code><\/pre>\n<p>This basic example uses only an XML template and report action.<\/p>\n<p>A Python report model and additional <code dir=\"ltr\">__init__.py<\/code> imports are not required unless the report needs custom data or calculations, which will be explained in Section 7.<\/p>\n<h3>Step 2: Configure the Manifest File<\/h3>\n<p>The <code dir=\"ltr\">__manifest__.py<\/code> file defines the module information and its dependencies.<\/p>\n<pre><code class=\"language-python\">{\r\n    'name': 'Custom Sale Report',\r\n    'version': '1.0',\r\n    'summary': 'Custom QWeb PDF report for sales orders',\r\n    'depends': ['sale'],\r\n    'data': [\r\n        'report\/sale_order_report.xml',\r\n    ],\r\n    'installable': True,\r\n    'application': False,\r\n    'license': 'LGPL-3',\r\n}<\/code><\/pre>\n<p>The <code dir=\"ltr\">sale<\/code> dependency ensures that the Sales application and its sales order model are available.<\/p>\n<p>The XML file is added to the <code dir=\"ltr\">data<\/code> list so that Odoo loads the report definition when installing or updating the module.<\/p>\n<h3>Step 3: Create the QWeb Report Template<\/h3>\n<p>Open <code dir=\"ltr\">report\/sale_order_report.xml<\/code> and define the template.<\/p>\n<pre><code class=\"language-xml\">&lt;odoo&gt;\r\n\r\n    &lt;template id=\"report_custom_sale_order\"&gt;\r\n\r\n        &lt;t t-call=\"web.html_container\"&gt;\r\n\r\n            &lt;t t-foreach=\"docs\" t-as=\"o\"&gt;\r\n\r\n                &lt;t t-call=\"web.external_layout\"&gt;\r\n\r\n                    &lt;div class=\"page\"&gt;\r\n\r\n                        &lt;h2&gt;Sales Order Report&lt;\/h2&gt;\r\n\r\n                        &lt;p&gt;\r\n                            &lt;strong&gt;Order Reference:&lt;\/strong&gt;\r\n                            &lt;span t-field=\"o.name\"\/&gt;\r\n                        &lt;\/p&gt;\r\n\r\n                        &lt;p&gt;\r\n                            &lt;strong&gt;Customer:&lt;\/strong&gt;\r\n                            &lt;span t-field=\"o.partner_id\"\/&gt;\r\n                        &lt;\/p&gt;\r\n\r\n                        &lt;p&gt;\r\n                            &lt;strong&gt;Order Date:&lt;\/strong&gt;\r\n                            &lt;span t-field=\"o.date_order\"\/&gt;\r\n                        &lt;\/p&gt;\r\n\r\n                        &lt;table class=\"table table-sm o_main_table\"&gt;\r\n\r\n                            &lt;thead&gt;\r\n                                &lt;tr&gt;\r\n                                    &lt;th&gt;Product&lt;\/th&gt;\r\n                                    &lt;th&gt;Quantity&lt;\/th&gt;\r\n                                    &lt;th&gt;Unit Price&lt;\/th&gt;\r\n                                    &lt;th&gt;Subtotal&lt;\/th&gt;\r\n                                &lt;\/tr&gt;\r\n                            &lt;\/thead&gt;\r\n\r\n                            &lt;tbody&gt;\r\n\r\n                                &lt;t t-foreach=\"o.order_line\"\r\n                                   t-as=\"line\"&gt;\r\n\r\n                                    &lt;tr&gt;\r\n                                        &lt;td&gt;\r\n                                            &lt;span t-field=\"line.product_id\"\/&gt;\r\n                                        &lt;\/td&gt;\r\n\r\n                                        &lt;td&gt;\r\n                                            &lt;span t-field=\"line.product_uom_qty\"\/&gt;\r\n                                        &lt;\/td&gt;\r\n\r\n                                        &lt;td&gt;\r\n                                            &lt;span t-field=\"line.price_unit\"\/&gt;\r\n                                        &lt;\/td&gt;\r\n\r\n                                        &lt;td&gt;\r\n                                            &lt;span t-field=\"line.price_subtotal\"\/&gt;\r\n                                        &lt;\/td&gt;\r\n                                    &lt;\/tr&gt;\r\n\r\n                                &lt;\/t&gt;\r\n\r\n                            &lt;\/tbody&gt;\r\n\r\n                        &lt;\/table&gt;\r\n\r\n                        &lt;div class=\"text-end\"&gt;\r\n                            &lt;strong&gt;Total Amount:&lt;\/strong&gt;\r\n                            &lt;span t-field=\"o.amount_total\"\/&gt;\r\n                        &lt;\/div&gt;\r\n\r\n                    &lt;\/div&gt;\r\n\r\n                &lt;\/t&gt;\r\n\r\n            &lt;\/t&gt;\r\n\r\n        &lt;\/t&gt;\r\n\r\n    &lt;\/template&gt;\r\n\r\n&lt;\/odoo&gt;<\/code><\/pre>\n<p><strong>Understanding the template:<\/strong><\/p>\n<ul data-spread=\"false\">\n<li><code dir=\"ltr\">docs<\/code> contains the records selected for printing.<\/li>\n<li><code dir=\"ltr\">t-foreach<\/code> loops through the selected sales orders.<\/li>\n<li><code dir=\"ltr\">o<\/code> represents the current sales order.<\/li>\n<li><code dir=\"ltr\">o.partner_id<\/code> retrieves the customer.<\/li>\n<li><code dir=\"ltr\">o.order_line<\/code> contains the order lines.<\/li>\n<li><code dir=\"ltr\">t-field<\/code> displays values using Odoo&#8217;s field-aware formatting.<\/li>\n<li><code dir=\"ltr\">web.external_layout<\/code> adds the standard company report layout.<\/li>\n<\/ul>\n<p>The result is a document that dynamically displays information from the selected sales order.<\/p>\n<h3>Step 4: Define the Report Action<\/h3>\n<p>Now add a report action to the same XML file.<\/p>\n<p>Place this record inside the <code dir=\"ltr\">&lt;odoo&gt;<\/code> root element, after the QWeb template.<\/p>\n<pre><code class=\"language-xml\">&lt;record id=\"action_report_custom_sale_order\"\r\n        model=\"ir.actions.report\"&gt;\r\n\r\n    &lt;field name=\"name\"&gt;Custom Sales Order&lt;\/field&gt;\r\n\r\n    &lt;field name=\"model\"&gt;sale.order&lt;\/field&gt;\r\n\r\n    &lt;field name=\"report_type\"&gt;qweb-pdf&lt;\/field&gt;\r\n\r\n    &lt;field name=\"report_name\"&gt;custom_sale_report.report_custom_sale_order&lt;\/field&gt;\r\n\r\n    &lt;field name=\"binding_model_id\"\r\n           ref=\"sale.model_sale_order\"\/&gt;\r\n\r\n    &lt;field name=\"print_report_name\"&gt;\r\n        'Sales Order - %s' % (object.name)\r\n    &lt;\/field&gt;\r\n\r\n&lt;\/record&gt;<\/code><\/pre>\n<p><strong>Important fields explained:<\/strong><\/p>\n<ul data-spread=\"false\">\n<li><code dir=\"ltr\">model<\/code>: Specifies that the report is associated with sales orders.<\/li>\n<li><code dir=\"ltr\">report_type<\/code>: Defines PDF as the output format.<\/li>\n<li><code dir=\"ltr\">report_name<\/code>: Identifies the QWeb template using its external ID.<\/li>\n<li><code dir=\"ltr\">binding_model_id<\/code>: Makes the report available from the sales order&#8217;s Print menu.<\/li>\n<li><code dir=\"ltr\">print_report_name<\/code>: Defines the filename using the current sales order reference.<\/li>\n<\/ul>\n<p>The <code dir=\"ltr\">binding_type<\/code> field defaults to <code dir=\"ltr\">report<\/code>, so it does not need to be explicitly added in this example.<\/p>\n<p>The <code dir=\"ltr\">report_file<\/code> field is also optional for this implementation.<\/p>\n<p><strong>Important:<\/strong> Keep the <code dir=\"ltr\">report_name<\/code> value on one line without additional whitespace. The template external ID must match exactly.<\/p>\n<h3>Step 5: Install or Update the Module<\/h3>\n<p>After completing the XML file:<\/p>\n<ol start=\"1\" data-spread=\"false\">\n<li>Restart the Odoo server if required.<\/li>\n<li>Open Odoo and activate Developer Mode.<\/li>\n<li>Navigate to Apps.<\/li>\n<li>Update the Apps List if the module is new.<\/li>\n<li>Search for <strong>Custom Sale Report<\/strong>.<\/li>\n<li>Install the module.<\/li>\n<\/ol>\n<p>If the module is already installed, update it so that Odoo loads the new XML definitions.<\/p>\n<h3>Step 6: Generate the PDF Report<\/h3>\n<p>After installation:<\/p>\n<ol start=\"1\" data-spread=\"false\">\n<li>Open the Sales application.<\/li>\n<li>Navigate to Orders.<\/li>\n<li>Open an existing sales order.<\/li>\n<li>Click the <strong>Print<\/strong> menu.<\/li>\n<li>Select <strong>Custom Sales Order<\/strong>.<\/li>\n<\/ol>\n<p>Odoo will execute the report action, process the QWeb template, retrieve the sales order data, and generate the PDF document.<\/p>\n<p>The generated report will contain the selected order&#8217;s details.<\/p>\n<h2>7. Customizing Reports with Python<\/h2>\n<p>The previous example uses the default report context provided by Odoo. This is sufficient for many reports.<\/p>\n<p>However, some business requirements need additional calculations or information from other models.<\/p>\n<p>For example, a custom report may need to display:<\/p>\n<ul data-spread=\"false\">\n<li>Additional product information.<\/li>\n<li>Related delivery records.<\/li>\n<li>Custom calculations.<\/li>\n<li>Aggregated information from multiple models.<\/li>\n<li>Data that is not directly available through the current record.<\/li>\n<\/ul>\n<p>For these situations, developers can create a custom report model using <code dir=\"ltr\">_get_report_values()<\/code>.<\/p>\n<h3>Example: Adding Custom Data to a Report<\/h3>\n<p>Create a Python file named <code dir=\"ltr\">sale_order_report.py<\/code>.<\/p>\n<p>The module structure now becomes:<\/p>\n<pre><code class=\"language-text\">custom_sale_report\/\r\n\u2502\r\n\u251c\u2500\u2500 __init__.py\r\n\u251c\u2500\u2500 __manifest__.py\r\n\u2502\r\n\u2514\u2500\u2500 report\/\r\n    \u251c\u2500\u2500 __init__.py\r\n    \u251c\u2500\u2500 sale_order_report.py\r\n    \u2514\u2500\u2500 sale_order_report.xml<\/code><\/pre>\n<p>Add the following code to <code dir=\"ltr\">sale_order_report.py<\/code>:<\/p>\n<pre><code class=\"language-python\">from odoo import models\r\n\r\n\r\nclass CustomSaleOrderReport(models.AbstractModel):\r\n\r\n    _name = 'report.custom_sale_report.report_custom_sale_order'\r\n    _description = 'Custom Sales Order Report'\r\n\r\n    def _get_report_values(self, docids, data=None):\r\n\r\n        docs = self.env['sale.order'].browse(docids)\r\n\r\n        quantities = {\r\n            order.id: sum(\r\n                order.order_line.mapped('product_uom_qty')\r\n            )\r\n            for order in docs\r\n        }\r\n\r\n        return {\r\n            'doc_ids': docids,\r\n            'doc_model': 'sale.order',\r\n            'docs': docs,\r\n            'quantities': quantities,\r\n        }<\/code><\/pre>\n<p>The method retrieves the selected sales orders and calculates the total ordered quantity separately for each order.<\/p>\n<p>This is important when multiple sales orders are printed together. Each order should display its own quantity rather than the combined quantity of every selected order.<\/p>\n<p>The returned dictionary makes the additional information available to the QWeb template.<\/p>\n<p>Display the calculated quantity inside the template:<\/p>\n<pre><code class=\"language-xml\">&lt;p&gt;\r\n    &lt;strong&gt;Total Ordered Quantity:&lt;\/strong&gt;\r\n    &lt;span t-out=\"quantities[o.id]\"\/&gt;\r\n&lt;\/p&gt;<\/code><\/pre>\n<p>Because <code dir=\"ltr\">quantities<\/code> is indexed by the current order&#8217;s ID, each report displays the correct quantity for that particular order.<\/p>\n<h3>Import the Python File<\/h3>\n<p>Add the following to <code dir=\"ltr\">report\/__init__.py<\/code>:<\/p>\n<pre><code class=\"language-python\">from . import sale_order_report<\/code><\/pre>\n<p>Then add the following to the module&#8217;s main <code dir=\"ltr\">__init__.py<\/code>:<\/p>\n<pre><code class=\"language-python\">from . import report<\/code><\/pre>\n<p>These imports ensure that Odoo loads the custom report model.<\/p>\n<p><strong>Important:<\/strong> When a custom report model is used, Odoo does not automatically include the standard <code dir=\"ltr\">docs<\/code>, <code dir=\"ltr\">doc_ids<\/code>, and <code dir=\"ltr\">doc_model<\/code> values in its returned report context. If the template needs them, they must be returned explicitly, as demonstrated above.<\/p>\n<h2>8. HTML Reports vs PDF Reports<\/h2>\n<p>Odoo supports both HTML and PDF output formats for QWeb reports.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>HTML Report<\/th>\n<th>PDF Report<\/th>\n<\/tr>\n<tr>\n<td>Report type<\/td>\n<td><code dir=\"ltr\">qweb-html<\/code><\/td>\n<td><code dir=\"ltr\">qweb-pdf<\/code><\/td>\n<\/tr>\n<tr>\n<td>Output<\/td>\n<td>HTML document<\/td>\n<td>PDF document<\/td>\n<\/tr>\n<tr>\n<td>Main use<\/td>\n<td>Browser preview<\/td>\n<td>Printing and sharing<\/td>\n<\/tr>\n<tr>\n<td>Layout<\/td>\n<td>HTML and CSS<\/td>\n<td>HTML\/CSS rendered as PDF<\/td>\n<\/tr>\n<tr>\n<td>Printing<\/td>\n<td>Can be printed from the browser<\/td>\n<td>Ready-to-use PDF output<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Both formats use QWeb templates. The main difference is how the final output is rendered and delivered.<\/p>\n<p>HTML reports are useful for previewing and testing a report&#8217;s layout. PDF reports are generally more suitable for official business documents that need to be downloaded, printed, or shared.<\/p>\n<h2>9. Paper Formats and Report Layout Customization<\/h2>\n<p>Different business documents require different page sizes and layouts.<\/p>\n<p>For example:<\/p>\n<ul data-spread=\"false\">\n<li>Invoices commonly use A4 paper.<\/li>\n<li>Product labels may use smaller custom dimensions.<\/li>\n<li>Receipts may require narrow paper formats.<\/li>\n<li>Shipping labels may require specific width and height settings.<\/li>\n<\/ul>\n<p>Odoo allows developers to define custom paper formats through the <code dir=\"ltr\">report.paperformat<\/code> model.<\/p>\n<h3>Example: Creating a Custom Paper Format<\/h3>\n<pre><code class=\"language-xml\">&lt;record id=\"paperformat_custom_a4\"\r\n        model=\"report.paperformat\"&gt;\r\n\r\n    &lt;field name=\"name\"&gt;Custom A4 Format&lt;\/field&gt;\r\n    &lt;field name=\"format\"&gt;A4&lt;\/field&gt;\r\n    &lt;field name=\"orientation\"&gt;Portrait&lt;\/field&gt;\r\n\r\n    &lt;field name=\"margin_top\"&gt;30&lt;\/field&gt;\r\n    &lt;field name=\"margin_bottom\"&gt;20&lt;\/field&gt;\r\n    &lt;field name=\"margin_left\"&gt;10&lt;\/field&gt;\r\n    &lt;field name=\"margin_right\"&gt;10&lt;\/field&gt;\r\n\r\n    &lt;field name=\"header_line\" eval=\"False\"\/&gt;\r\n    &lt;field name=\"header_spacing\"&gt;20&lt;\/field&gt;\r\n\r\n&lt;\/record&gt;<\/code><\/pre>\n<p>The paper format record must be loaded before the report action that references it.<\/p>\n<p>For example, if both records are in the same XML file, place the paper format record before the report action.<\/p>\n<p>Then connect the paper format to the report action:<\/p>\n<pre><code class=\"language-xml\">&lt;field name=\"paperformat_id\"\r\n       ref=\"custom_sale_report.paperformat_custom_a4\"\/&gt;<\/code><\/pre>\n<p>The report action can now use the defined paper size, orientation, and margins.<\/p>\n<h3>Customizing Report Appearance with CSS<\/h3>\n<p>Developers can also customize report appearance using CSS.<\/p>\n<p>For example, the following styles can be added inside the QWeb template:<\/p>\n<pre><code class=\"language-xml\">&lt;style&gt;\r\n    .report-title {\r\n        font-size: 24px;\r\n        font-weight: bold;\r\n        text-align: center;\r\n    }\r\n\r\n    .report-table {\r\n        width: 100%;\r\n        border-collapse: collapse;\r\n    }\r\n\r\n    .report-table th,\r\n    .report-table td {\r\n        border: 1px solid #ddd;\r\n        padding: 8px;\r\n    }\r\n&lt;\/style&gt;<\/code><\/pre>\n<p>The corresponding classes can be applied to HTML elements:<\/p>\n<pre><code class=\"language-xml\">&lt;h2 class=\"report-title\"&gt;Sales Order Report&lt;\/h2&gt;\r\n\r\n&lt;table class=\"report-table\"&gt;\r\n    &lt;!-- Report table content --&gt;\r\n&lt;\/table&gt;<\/code><\/pre>\n<p>For report-specific fonts and assets, Odoo provides the <code dir=\"ltr\">web.report_assets_common<\/code> asset bundle.<\/p>\n<p>Custom fonts should be added through this bundle rather than assuming that backend or general website assets will automatically be available in PDF reports.<\/p>\n<h3>PDF CSS Compatibility<\/h3>\n<p>An important point to remember is that <code dir=\"ltr\">wkhtmltopdf<\/code> uses an older browser rendering engine.<\/p>\n<p>Modern CSS features such as CSS Grid and some newer layout properties may not render correctly in generated PDFs.<\/p>\n<p>For reliable PDF layouts:<\/p>\n<ul data-spread=\"false\">\n<li>Prefer simple HTML structures.<\/li>\n<li>Use tables for tabular information.<\/li>\n<li>Test margins and page breaks.<\/li>\n<li>Avoid relying heavily on modern browser-only CSS features.<\/li>\n<li>Verify the final PDF rather than depending only on the HTML preview.<\/li>\n<\/ul>\n<h2>10. Troubleshooting QWeb Reports<\/h2>\n<p>Developers may encounter different issues while creating or customizing reports. Understanding their causes makes troubleshooting easier.<\/p>\n<h3>10.1 Report Not Appearing in the Print Menu<\/h3>\n<p><strong>Possible causes:<\/strong><\/p>\n<ul data-spread=\"false\">\n<li>The report action is missing.<\/li>\n<li><code dir=\"ltr\">binding_model_id<\/code> is not configured correctly.<\/li>\n<li>The report action is linked to the wrong model.<\/li>\n<li>The report is restricted by <code dir=\"ltr\">groups_id<\/code>.<\/li>\n<li>The module has not been updated.<\/li>\n<\/ul>\n<p><strong>Solution:<\/strong> Verify the report action, model binding, and user group permissions. Check whether the current user belongs to a group permitted to access the report.<\/p>\n<h3>10.2 QWeb Template Not Found<\/h3>\n<p>This usually happens when the external ID in <code dir=\"ltr\">report_name<\/code> does not match the template&#8217;s external ID.<\/p>\n<p><strong>Solution:<\/strong> Check the module name and template ID carefully.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-text\">custom_sale_report.report_custom_sale_order<\/code><\/pre>\n<p>The module name and template ID must be correct.<\/p>\n<h3>10.3 Fields Not Displaying in the Report<\/h3>\n<p>This may happen when:<\/p>\n<ul data-spread=\"false\">\n<li>The field name is incorrect.<\/li>\n<li>The field does not exist on the selected model.<\/li>\n<li>The wrong record variable is being used.<\/li>\n<li>The required value is not included in the custom report context.<\/li>\n<\/ul>\n<p><strong>Solution:<\/strong> Verify the model&#8217;s field names and inspect the report&#8217;s rendering context.<\/p>\n<h3>10.4 PDF Layout Is Different from the HTML Preview<\/h3>\n<p>A report may look correct in the browser but appear differently in the generated PDF.<\/p>\n<p>This can happen because HTML and PDF rendering do not always handle CSS, page dimensions, and external resources in exactly the same way.<\/p>\n<p><strong>Solution:<\/strong> Test the PDF output regularly, check the paper format, and avoid relying on unsupported or browser-specific styling.<\/p>\n<h4>Previewing a Report in HTML<\/h4>\n<p>Odoo provides an HTML report route that developers can use to inspect report output.<\/p>\n<p>The general URL format is:<\/p>\n<pre><code class=\"language-text\">\/report\/html\/&lt;report_name&gt;\/&lt;record_id&gt;<\/code><\/pre>\n<p>For example:<\/p>\n<pre><code class=\"language-text\">\/report\/html\/custom_sale_report.report_custom_sale_order\/15<\/code><\/pre>\n<p>Replace <code dir=\"ltr\">15<\/code> with the ID of an actual sales order in the database.<\/p>\n<p>The HTML route helps developers inspect the generated report before converting it into a PDF.<\/p>\n<p>Activate Developer Mode when troubleshooting, and use the browser&#8217;s developer tools to inspect HTML elements and styles.<\/p>\n<p>You can also compare the HTML output with the generated PDF to identify rendering differences.<\/p>\n<h3>10.5 Custom Fonts Are Not Appearing<\/h3>\n<p>Custom fonts may not load if they are added only to the backend or website asset bundles.<\/p>\n<p><strong>Solution:<\/strong> Include the font and its related styles in <code dir=\"ltr\">web.report_assets_common<\/code> and verify that the font file is accessible.<\/p>\n<h3>10.6 Report Filename Contains Unexpected Characters<\/h3>\n<p>Some Odoo document references contain forward slashes, such as invoice references.<\/p>\n<p>When generating a filename, these characters may cause unwanted filename behaviour.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-xml\">&lt;field name=\"print_report_name\"&gt;\r\n    'Invoice - %s' % (object.name.replace('\/', ''))\r\n&lt;\/field&gt;<\/code><\/pre>\n<p>Removing forward slashes helps create a cleaner filename.<\/p>\n<h2>11. Working with Translated Reports<\/h2>\n<p>Businesses operating in multiple countries may need reports in different languages.<\/p>\n<p>Odoo supports translated report content through its translation mechanisms and language-aware record handling.<\/p>\n<p>When a report contains translatable fields, developers should ensure that the record is accessed in the intended customer&#8217;s language.<\/p>\n<p>For example, a report may need to display product names and descriptions in the customer&#8217;s preferred language.<\/p>\n<p>A common approach is to re-browse the record using the appropriate language context:<\/p>\n<pre><code class=\"language-xml\">&lt;t t-set=\"o\"\r\n   t-value=\"o.with_context(lang=o.partner_id.lang)\"\/&gt;<\/code><\/pre>\n<p>This makes the current record available in the customer&#8217;s language context for the subsequent template rendering.<\/p>\n<p>The correct language depends on the business requirement and the language configured for the customer.<\/p>\n<h2>12. Best Practices for Developing QWeb Report<\/h2>\n<p>A well-designed report should be easy to read, maintain, and reuse.<\/p>\n<p>Consider the following practices:<\/p>\n<ol start=\"1\" data-spread=\"false\">\n<li><strong>Keep templates organized:<\/strong> Store report XML files in a dedicated <code dir=\"ltr\">report\/<\/code> directory.<\/li>\n<li><strong>Use the correct directives:<\/strong> Prefer <code dir=\"ltr\">t-field<\/code> for Odoo record fields and <code dir=\"ltr\">t-out<\/code> for general expression output.<\/li>\n<li><strong>Avoid unnecessary calculations in XML:<\/strong> Move complex business calculations into Python when appropriate.<\/li>\n<li><strong>Reuse standard layouts:<\/strong> Use Odoo&#8217;s standard external layout when it meets the document&#8217;s requirements.<\/li>\n<li><strong>Test with different records:<\/strong> Check reports containing multiple lines, long product names, missing optional values, and different amounts.<\/li>\n<li><strong>Check page breaks:<\/strong> Verify that tables and important sections are displayed correctly across multiple pages.<\/li>\n<li><strong>Follow access rights:<\/strong> Ensure that users who can print a report are permitted to access the underlying business information.<\/li>\n<li><strong>Use template inheritance carefully:<\/strong> When modifying an existing report, prefer supported inheritance and XPath expressions instead of replacing the entire template unnecessarily.<\/li>\n<li><strong>Test PDF output:<\/strong> HTML previews alone are not sufficient to confirm the final PDF layout.<\/li>\n<li><strong>Consider translations:<\/strong> Ensure that the correct language context is used when generating customer-facing documents.<\/li>\n<\/ol>\n<h2>13. Frequently Asked Questions<\/h2>\n<h3>What is QWeb in Odoo 20?<\/h3>\n<p>QWeb is Odoo&#8217;s XML-based templating engine. It is used to generate dynamic HTML content and reports by combining XML structures, template directives, and business data.<\/p>\n<h3>What is the difference between a QWeb template and a report action?<\/h3>\n<p>A QWeb template defines the report&#8217;s structure and appearance. A report action connects the template to a business model and defines how the report is generated and accessed.<\/p>\n<h3>Can we generate PDF reports using QWeb?<\/h3>\n<p>Yes. Odoo supports PDF reports through the <code dir=\"ltr\">qweb-pdf<\/code> report type. It uses the QWeb template to generate HTML and <code dir=\"ltr\">wkhtmltopdf<\/code> to produce the final PDF document.<\/p>\n<h3>Can we create custom reports in Odoo 20?<\/h3>\n<p>Yes. Developers can create custom reports using XML templates, report actions, and Python models when additional data or calculations are required.<\/p>\n<h3>What is the purpose of <code dir=\"ltr\">web.external_layout<\/code>?<\/h3>\n<p>It provides the standard external report layout, including company header and footer elements.<\/p>\n<h3>What is the purpose of <code dir=\"ltr\">docs<\/code> in a QWeb report?<\/h3>\n<p><code dir=\"ltr\">docs<\/code> contains the records being processed by the report. For example, when printing a sales order report, it contains the selected sales order records.<\/p>\n<h3>Can we customize the appearance of an existing Odoo report?<\/h3>\n<p>Yes. Developers can use QWeb template inheritance and XPath expressions to extend or modify existing report templates without unnecessarily duplicating the complete original template.<\/p>\n<h3>Can QWeb report display information from multiple models?<\/h3>\n<p>Yes. Related fields can often be accessed through the current record. For more complex requirements, developers can prepare additional data using a custom report model and <code dir=\"ltr\">_get_report_values()<\/code>.<\/p>\n<h3>Why is my custom report not visible in the Print menu?<\/h3>\n<p>Check the report action&#8217;s model, <code dir=\"ltr\">binding_model_id<\/code>, and user group restrictions. Also ensure that the module has been installed or updated successfully.<\/p>\n<h3>Can I preview a QWeb report without generating a PDF?<\/h3>\n<p>Yes. Developers can use the HTML report route:<\/p>\n<p><code dir=\"ltr\">\/report\/html\/&lt;report_name&gt;\/&lt;record_id&gt;<\/code><\/p>\n<p>This is useful for inspecting report structure and troubleshooting layout issues.<\/p>\n<h2>Conclusion<\/h2>\n<p>QWeb templates are a fundamental part of Odoo&#8217;s reporting framework. They allow developers to create dynamic documents using XML, template directives, and business data stored in Odoo.<\/p>\n<p>By combining QWeb templates with report actions, developers can build custom PDF and HTML reports for different business requirements. Features such as template inheritance, reusable sub-templates, custom Python report models, paper formats, and language-aware rendering provide additional flexibility when developing advanced documents.<\/p>\n<p>Understanding how these components work together is useful for Odoo developers who want to customize existing reports or build new reporting functionality for business applications.<\/p>\n<p>Whether you are creating a simple sales order document or a complex report that combines information from multiple models, understanding QWeb provides the foundation for developing flexible and maintainable reports in Odoo 20.<\/p>\n<h2>References and Further Reading<\/h2>\n<ol start=\"1\" data-spread=\"false\">\n<li><a href=\"https:\/\/www.odoo.com\/documentation\/20.0\/developer\/reference\/frontend\/qweb.html\" target=\"_blank\" rel=\"noopener\">Odoo 20 \u2013 QWeb Templates<\/a><\/li>\n<li><a href=\"https:\/\/www.odoo.com\/documentation\/20.0\/developer\/reference\/backend\/reports.html\" target=\"_blank\" rel=\"noopener\">Odoo 20 \u2013 QWeb Report<\/a><\/li>\n<li><a href=\"https:\/\/www.odoo.com\/documentation\/20.0\/developer\/tutorials\/pdf_reports.html\" target=\"_blank\" rel=\"noopener\">Odoo 20 \u2013 PDF Reports Tutorial<\/a><\/li>\n<li><a href=\"https:\/\/www.odoo.com\/documentation\/20.0\/developer\/reference\/backend\/actions.html\" target=\"_blank\" rel=\"noopener\">Odoo 20 \u2013 Actions and Report Actions<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Reports are an essential part of everyday business operations. Businesses use them to generate invoices, quotations, sales orders, delivery documents, purchase orders, employee records, and many other documents. Odoo provides a flexible reporting system that allows developers to generate these documents using information already stored in the database. One of the main technologies behind this [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":10184,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[325,195],"tags":[],"class_list":["post-10183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odoo-20","category-odoo-customization"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/machinser.com\/wp-content\/uploads\/2026\/11\/QWeb-Templates-in-Odoo-20.png","_links":{"self":[{"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/posts\/10183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/comments?post=10183"}],"version-history":[{"count":1,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/posts\/10183\/revisions"}],"predecessor-version":[{"id":10185,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/posts\/10183\/revisions\/10185"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/media\/10184"}],"wp:attachment":[{"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/media?parent=10183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/categories?post=10183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinser.com\/ar\/wp-json\/wp\/v2\/tags?post=10183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}