Enable Dark Mode!
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 system which will include pdf reports and many other necessary documents and files. We know documents are necessary for the business organization. Moreover, an executive spends more than half of the time is utilized for managing the documents. So it is very important to organize them in the first place.

As an Odoo user, we know the importance of attachments. So in Odoo we basically use the uploading method to attach the documents. That is when we normally create an attachment, then we must manually upload it on record. However, we can also create a pdf attachment report automatically in OdooOdoo and solve the manual operations needed.

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

Before that, first, we make sure that “wkhtmltopdf “ already installed in your system if not install it.

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


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'

In the above command, we can see a model named “ir.attachment”, which by default all the attachments are managed by this model. So we can create the ir.attachment model by using the create method.

As the attachment is now configured to be created automatically now we need a button to execute this function. So let’s define it in the XML file using the following code:

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


<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. Moreover, inside the function we can call the report reference id for the qweb-pdf report. So we need the following code inside the report XML file.


<report id="report_id"
string="Model Report"

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

The template for the report is as follows, additionally, we can customize the contents that should be depicted inside the report. 

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

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


The generated pdf for the corresponding model is shown in the chatter section as depicted in the following screenshot.


Furthermore, we can also view all the generated documents from the settings of the Odoo platform, to view it use the following navigation:

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


The following image shows the corresponding record of created attachment.


Attachment auto-generation features using a simple button function will be much helpful in the functioning of the company. Furthermore, it will reduce the manual aspects of report generation. Hope the blog turned useful.

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



it works perfectly you're great





Great, I need it strongly. See no good document except this article. Thank you very much.





Is correcto this line in Odoo 13?? 'datas_fname': name + '.pdf',





There is no 'datas_fname' field in Odoo 13. This part causes error: 'datas_fname': name + '.pdf', Use: 'name': name + '.pdf', This info could save 1 hour of my time :)




Leave a comment




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



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



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

Send Us A Message