how-to-create-pdf-report-odoo-13.png
Blogger_637091950955761141.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.Model):
_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 s_l
            JOIN sale_order_line AS s_o_l ON s_l.id = s_o_l.sale_order_id
            WHERE s_l.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.

So 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.



cybrosys youtube

Comments

0


Leave a comment

 
Calicut

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

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

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

Bangalore

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

Send Us A Message