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


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.


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


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

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

When we click on the Generate Attachment button(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.


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

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


The below image shows the corresponding record of created attachment.


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

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

cybrosys youtube



Is it programmatically create a PDF attachment in Odoo. I would like to have a custom report which needs to be added as an attachment. So I'm looking for a method to generate a real PDF attachment. I have talked with many Odoo Consultant they couldn't provide me a solution. Thank you





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




Leave a comment


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


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


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