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.
""" 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)
# save pdf as attachment
name = "My Attachment"
'datas_fname': name + '.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.
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.
<t t-foreach="docs" t-as="doc">
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.