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.
.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'
})
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.
.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. 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.
.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"/>
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
-->
</t>
</template>
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.