creating-pdf-attachment-automatically-odoo-13.png
Blogger_637129942863783933.jpg
By: Varsha Vivek K

How to Create Pdf Attachment Automatically in Odoo 13

Technical Odoo 13

Attachments are the files or documents which are uploaded on your page. These are embedded in the pdf reports. We know documents are necessary for the business organization. A business man’s half of the time is utilized for managing the documents. So it is very important to organize them.


As an Odoo user, we know the importance of attachments. So in Odoo basically we use the uploading method to attach the documents. That is when we normally create an attachment, then we must manually upload it on record. But we can also create a pdf attachment report automatically in Odoo. It is very easy to do. But many may not be aware of how doing it. 


In this blog, I am going to explain the process of automatically creating a pdf attachment report by a button click.


Before that, first, we make sure that “wkhtmltopdf “ already installed in your system.


Then define a function inside the model(.py) where you want to automatically generate the pdf attachment report.


.py 

def action_get_attachment(self):
    """ this method called from button action in view xml ""”
    pdf = self.env.ref('module_name..report_id').render_qweb_pdf(self.ids)
    b64_pdf = base64.b64encode(pdf[0])
    # save pdf as attachment
    name = "My Attachment"
    return self.env['ir.attachment'].create({
        'name': name,
        'type': 'binary',
        'datas': b64_pdf,
        'datas_fname': name + '.pdf',
        'store_fname': name,
        'res_model': self._name,
        'res_id': self.id,
        'mimetype': 'application/x-pdf'
    })


The above function helps us to create the attachment record automatically. Here we can see a model named “ir.attachment”, By default all the attachments are managed by this model. So we can create the ir.attachment model by using the create method.


Now we need a button to execute this function. So let’s define it in the XML file.


.XML  

<button name="action_get_attachment" string="Generate Attachment" type="object"/>


When the button click is enabled, then the corresponding function in the python file gets executed. Inside the function, we can call the report reference id for the qweb-pdf report. So we need the below code inside the report XML file.


.XML

<report id="report_id"
model="model.name"
string="Model Report"
report_type="qweb-pdf"
name="module_name.template_id"
file="module_name.template_id"
menu="False"/>


And to generate a pdf report we need to create a template for qweb report. This template is called from the above record.


The template for the report is as follows, and we can decide the content to be inside the report.

<template id="template_id">
    <t t-foreach="docs" t-as="doc">
        <!--
            Document content
        -->
    </t>
</template>


When we click on the Generate Attachment button(shown below), it will automatically generate a pdf attachment based on the report template.


creating-pdf-attachment-automatically-odoo-13-cybrosys


The generated pdf for the corresponding model is shown in the chatter section.


creating-pdf-attachment-automatically-odoo-13-cybrosys


And we can also view all the generated documents from the settings of Odoo.


Go to Settings -> Technical -> Database Structure -> Attachments


creating-pdf-attachment-automatically-odoo-13-cybrosys


The below image shows the corresponding record of created attachment.


creating-pdf-attachment-automatically-odoo-13-cybrosys


This is how we automatically generate an attachment in Odoo. Hope the blog turned useful.



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