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

