Web Development

Creating Custom Reports in PDF – Odoo 17.0 Documentation

This guide details creating custom PDF reports in Odoo 17.0. Improve your business processes with expert Odoo development services in South Africa provided by Shiv Technolabs, ensuring tailored and professional solutions for all your Odoo needs.

Creating custom reports in PDF format is a crucial aspect of Odoo 17.0. It allows businesses to generate professional and tailored documents that suit their unique needs. This guide provides a step-by-step process for creating custom PDF reports in Odoo 17.0.

Odoo 17.0 offers a flexible and powerful reporting engine that enables users to create custom reports in various formats, including PDF. Creating custom reports in Odoo provides businesses with the ability to generate documents that are tailored to their specific needs, ensuring that the information presented is both relevant and professionally formatted. This tutorial will guide you through the process of creating custom PDF reports using Odoo’s reporting framework.

Custom reports in Odoo can be used for a variety of purposes, including invoicing, sales orders, purchase orders, and many other business documents. By customizing these reports, you can ensure that they match your company’s branding and include all necessary information.

Setting Up Your Development Environment


Setting Up Your Development Environment odoo

Before starting, ensure that your development environment is set up correctly. You need the following prerequisites:

  • Odoo 17.0 Installed: Make sure you have Odoo 17.0 installed on your server or local machine.
  • Access to the Odoo Database: Ensure you have access to the Odoo database where you will be creating the custom reports.
  • Basic Knowledge of Python and XML: Familiarity with Python and XML is necessary as Odoo’s reporting engine relies on these technologies.
  • An IDE (Integrated Development Environment): Use an IDE such as Visual Studio Code to facilitate coding and managing files.

To begin, set up a development environment on your machine. This environment will be used to create and test your custom reports. Make sure you have all the necessary permissions to access and modify the Odoo instance.

Creating a Custom Module


To create custom reports in Odoo, you first need to create a custom module. Follow these steps:

# Create the Module Structure:

  • Navigate to your Odoo add-ons directory.
  • Create a new directory for your module, e.g., “custom_report”.
  • Inside this directory, create the following subdirectories: “models”, “views”, “reports”, and “data”.

# Create the Manifest File (“__manifest__.py”):

In the “custom_report” directory, create a file named “__manifest__.py”.

Add the following content to the file:

{
    'name': 'Custom Report',
    'version': '1.0',
    'summary': 'Module for Custom PDF Reports',
    'category': 'Tools',
    'author': 'Your Name',
    'depends': ['base'],
    'data': [
        'views/report_templates.xml',
        'reports/report_action.xml',
        'data/report_paperformat.xml',
    ],
}

This manifest file provides Odoo with essential information about your module, including its name, version, dependencies, and the data files it uses. The data files will include your report templates and report actions.

# Create the Init File (“__init__.py”):

In the “custom_report” directory, create a file named “__init__.py”.

Add the following content:

from . import models

This “__init__.py” file initializes the models for your custom module. If your report needs specific models, you will define them in the “models” directory and import them here.

Defining the Report Template


Defining the Report Template

Report templates in Odoo are defined using QWeb, a templating engine that uses XML syntax. QWeb is a powerful templating engine that allows you to create complex and dynamic templates for your reports. Follow these steps to create a report template:

# Create the Report Template:

In the “views” directory, create a file named “report_templates.xml”.

Add the following content to define a basic report template:

<odoo>
    <template id="report_custom_pdf">
        <t t-call="web.basic_layout">
            <t t-set="doc_ids" t-value="docids"/>
            <t t-set="doc_model" t-value="doc_model"/>
            <t t-set="docs" t-value="docs"/>
            <div class="page">
                <h2>Custom PDF Report</h2>
                <p>Date: <span t-esc="time.strftime('%Y-%m-%d')"/></p>
                <p t-foreach="docs" t-as="doc">
                    <span t-esc="doc.name"/>: <span t-esc="doc.description"/>
                </p>
            </div>
        </t>
    </template>
</odoo>

This basic template includes a title and a loop that iterates over the documents to display their names and descriptions. You can customize this template to include any information relevant to your report.

Adding Report Actions


Next, you need to define the report actions that will generate the PDF report. Report actions tell Odoo how to process and render your report.

# Create the Report Action File:

In the “reports” directory, create a file named “report_action.xml”.

Add the following content:

<odoo>
    <report
        id="action_report_custom_pdf"
        model="your.model"
        string="Custom PDF Report"
        report_type="qweb-pdf"
        file="custom_report.report_custom_pdf"
        name="custom_report.report_custom_pdf"/>
</odoo>

This file defines a report action that links your report template to the model it will be used with. The “report_type” is set to “qweb-pdf”, indicating that the report will be generated in PDF format.

Registering the Report


You need to register the report in the Odoo system. This involves creating a record in the “ir.actions.report” model. This record tells Odoo how to generate the report and what template to use.

# Create the Report Data File:

In the “data” directory, create a file named “report_paperformat.xml”.

Add the following content to define the paper format:

<odoo>
    <record id="paperformat_custom_pdf" model="report.paperformat">
        <field name="name">Custom PDF Report</field>
        <field name="default" eval="True"/>
        <field name="format">A4</field>
        <field name="margin_top">20</field>
        <field name="margin_bottom">20</field>
        <field name="margin_left">10</field>
        <field name="margin_right">10</field>
        <field name="orientation">Portrait</field>
        <field name="header_spacing">10</field>
    </record>
</odoo>

This file defines the paper format for your report, including the size (A4), margins, and orientation. You can customize these settings based on your requirements.

Customizing the Report with QWeb


QWeb allows you to customize the report layout and content. You can use various HTML tags and Odoo-specific directives to tailor the report. Customizing the report with QWeb gives you the flexibility to create detailed and professional-looking documents.

# Enhance the Report Template:

Modify “report_templates.xml” to include more detailed information and formatting:

<odoo>
    <template id="report_custom_pdf">
        <t t-call="web.basic_layout">
            <t t-set="doc_ids" t-value="docids"/>
            <t t-set="doc_model" t-value="doc_model"/>
            <t t-set="docs" t-value="docs"/>
            <div class="page">
                <h2>Custom PDF Report</h2>
                <table class="table table-sm table-striped">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <t t-foreach="docs" t-as="doc">
                            <tr>
                                <td><span t-esc="doc.id"/></td>
                                <td><span t-esc="doc.name"/></td>
                                <td><span t-esc="doc.description"/></td>
                            </tr>
                        </t>
                    </tbody>
                </table>
            </div>
        </t>
    </template>
</odoo>

This enhanced template includes a table to organize the report data. The table structure provides a clear and organized layout for the report, making it easier to read and understand.

Adding Styles to the Report


You can add custom styles to your PDF report using CSS. This enhances the visual appearance of the report, making it more professional and aligned with your brand.

# Add Custom Styles:

Modify “report_templates.xml” to include a style section:

<odoo>
    <template id="report_custom_pdf">
        <t t-call="web.basic_layout">
            <t t-set="doc_ids" t-value="docids"/>
            <t t-set="doc_model" t-value="doc_model"/>
            <t t-set="docs" t-value="docs"/>
            <t t-set="date" t-value="time.strftime('%Y-%m-%d')"/>
            <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.page%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A%20Arial%2C%20sans-serif%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20h2%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text-align%3A%20center%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3A%20%234CAF50%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20table%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3A%20100%25%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border-collapse%3A%20collapse%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20th%2C%20td%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20padding%3A%208px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text-align%3A%20left%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border-bottom%3A%201px%20solid%20%23ddd%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20th%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background-color%3A%20%23f2f2f2%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />
            <div class="page">
                <h2>Custom PDF Report</h2>
                <p>Date: <span t-esc="date"/></p>
                <table class="table table-sm table-striped">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <t t-foreach="docs" t-as="doc">
                            <tr>
                                <td><span t-esc="doc.id"/></td>
                                <td><span t-esc="doc.name"/></td>
                                <td><span t-esc="doc.description"/></td>
                            </tr>
                        </t>
                    </tbody>
                </table>
            </div>
        </t>
    </template>
</odoo>

Adding custom styles ensures that your report not only contains the necessary information but also looks aesthetically pleasing. This can include adjusting fonts, colors, table styles, and more.

Testing and Debugging


After setting up your custom report, it is essential to test and debug to ensure everything works as expected.

# Testing the Report:

  • Go to the Odoo interface and navigate to the model associated with your report.
  • Generate the report to see if it appears correctly.

# Debugging Common Issues:

  • If the report does not generate, check the Odoo logs for errors.
  • Ensure that all XML files are correctly formatted and that all dependencies are met.
  • Verify that the report template and report actions are correctly defined.

Testing and debugging are crucial steps in the process of creating custom reports. They help identify and fix issues, ensuring that the final report meets your expectations and requirements.

Conclusion


Creating custom PDF reports in Odoo 17.0 involves several steps, from setting up your development environment to defining templates and styles. By following this guide, you can create professional and tailored reports that meet your specific business needs. Custom reports in Odoo allow for a high degree of customization, enabling you to present information in a clear, organized, and visually appealing manner.

An Odoo development company in South Africa, such as Shiv Technolabs, can provide expert assistance in creating custom reports and other tailored Odoo solutions. With specialized Odoo development services in South Africa, businesses can leverage the full potential of Odoo to streamline their operations, enhance their reporting capabilities, and improve overall efficiency.

Partnering with a professional Odoo development company in South Africa ensures that your custom reports are developed with the highest standards of quality and precision. Shiv Technolabs offers comprehensive Odoo development services in South Africa, providing tailored solutions that meet the unique requirements of your business. Whether you need custom reports, module development, or system integration, Shiv Technolabs is equipped to deliver exceptional results.

background-line

Revolutionize Your Digital Presence with Our Mobile & Web Development Service. Trusted Expertise, Innovation, and Success Guaranteed.

Written by

Dipen Majithiya

I am a proactive chief technology officer (CTO) of Shiv Technolabs. I have 10+ years of experience in eCommerce, mobile apps, and web development in the tech industry. I am Known for my strategic insight and have mastered core technical domains. I have empowered numerous business owners with bespoke solutions, fearlessly taking calculated risks and harnessing the latest technological advancements.