Enable Dark Mode!
how-to-create-pdf-report-odoo-13.png
By: Yadukrishna K

How to Create a Pdf Report in Odoo 13

Technical Odoo 13

A report is a document that can present information about a company or organization in an organized format for a specific audience and purpose. Reports play a significant role in the growth of the business. Odoo uses two types of report types are qweb-pdf and qweb-html. The easiest and useful report is qweb-pdf. So odoo uses qweb in more cases to access data from models and get a perfect view for the report.
Here we use the qweb-pdf report type for this operation. So I created a button and created a function with the button name.
In python file( module_name > models > .py )

def button_name(self):
data = {
'model_id': self.id,
}
return self.env.ref(module_name.print_report_pdf').report_action(self, data=data)
Here I am using a code to trigger an XML part so I specified the model name and id of the XML part.
After this code creates a directory named as report inside the module and creates an xml file.
Then write this snippet on the XML file. The id of this report tag must be equal to the reference that we specified earlier in the python part, not the name of the XML file.

In xml part (module_name > report > .xml)
<odoo>
<report id="print_report_pdf'"
string="Sample Report"
model="model_name"
report_type="qweb-pdf"
name="module_name.print_sample_report"
file="module_name.print_sample_report"
menu="False"/>
</odoo>
After this xml part creates a python file inside the report directory.(eg:- module_name > report > .py)

You must be bothered about specifying the name of the model. In the above XML part, one can see an attribute name. You should use the string to create a model name by adding a report before this string.

In python file
class SampleReportPrint(models.AbstractModel):
    _name = 'report.module_name.print_sample_report'
    @api.model
    def _get_report_values(self, docids, data):
        """in this function can access the data returned from the button
        click function"""
        model_id = data['model_id']
        value = []
        query = """SELECT *
                        FROM sale_order as so
                        JOIN sale_order_line AS sl ON so.id = sl.sale_order_id
                        WHERE sl.id = %s"""
        value.append(model_id)
        self._cr.execute(query, value)
        record = self._cr.dictfetchall()
        return {
            'docs': record,
            'date_today': fields.Datetime.now(),
        }
After writing the python code I have to create a template for the print report.
To create an XML file for the report template. This template helps to get data from the report model and also can align content like HTML.
In report template part ( module_name > report > .XML )
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <template id="print_sample_report">
            <t t-call="web.html_container">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h1 align="center">
                            Sample Report
                        </h1>
                        <t t-forech="docs" t-as="line">
                            <h3>
                                <t t-esc="line['name']"/>
                            </h3>
                            <h3>
                                <t t-esc="date_today"/>
                            </h3>
                        </t>
                    </div>
                </t>
            </t>
        </template>
    </data>
</odoo>
In this template, I used a loop for getting values from the variable docs.
You must check all the files paths added to the manifest file and update odoo then install your module.


If you need any assistance in odoo, we are online, please chat with us.



2
Comments

Maicoly

Hi @Yadukrishna, I have an error Traceback (most recent call last): File "/home/maicoly/boxbeep/pyrhonoddo/odoo13-boxbeep/addons/web/controllers/main.py", line 2032, in report_download response = self.report_routes(reportname, converter=converter, context=context, **data) File "/home/maicoly/boxbeep/pyrhonoddo/odoo13-boxbeep/odoo/http.py", line 515, in response_wrap response = f(*args, **kw) File "/home/maicoly/boxbeep/pyrhonoddo/odoo13-boxbeep/addons/web/controllers/main.py", line 1963, in report_routes pdf = report.with_context(context).render_qweb_pdf(docids, data=data)[0] File "/home/maicoly/boxbeep/pyrhonoddo/odoo13-boxbeep/odoo/addons/base/models/ir_actions_report.py", line 741, in render_qweb_pdf if self.attachment and set(res_ids) != set(html_ids): TypeError: 'NoneType' object is not iterable please help me!

28/09/2020

-

5:07PM

Fasil

I have a Qweb report in "res.partner" I need to call this report based on related field (partner_id) from "account.invoice" Please help to get python function for this requirement..

10/08/2020

-

5:04AM



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message