Enable Dark Mode!
how-to-create-pdf-attachment-automatically-in-odoo-15.jpg
By: Alakananda P

How to Create Pdf Attachment Automatically in Odoo 15

Technical Odoo 15

Documents or files that can be uploaded on a system are called Attachments, including pdf reports, xlsx reports, etc. As we all know that documents are important for the business organization. An executive spends a lot of time and effort for managing documents in an organization. So, it is necessary to organize the documents in the first place.

In odoo, the uploading method is basically used to attach the documents. That means, when we create an attachment normally, we have to upload the record manually. We can automatically solve its manual operation by creating a pdf report in odoo.

In this blog, we will discuss how to create a pdf attachment automatically with the help of Odoo 15.

Before that, make sure whether “wkhtmltopdf “ is already installed or not. If it is not installed , you should install it first.

The below function is used to define how to generate automatic pdf attachment reports inside a model.

Models > Python Code:

import base64
from odoo import models
class AccountMoveInherit(models.Model):
   _inherit = 'account.move'
   def action_get_attachment(self):
       """ This method is used to generate attachment for pdf report"""
       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,
           'store_fname': name,
           'res_model': self._name,
           'res_id': self.id,
           'mimetype': 'application/x-pdf'
       })

As mentioned above, the ‘ir.attachment’ model manages all the default attachments in the corresponding model. So the ‘create’ method is used to create a new ‘ir.attachment’ record.

This ‘action_get_attachment’ function is executed before the attachment is created automatically. So let’s define a button inside the XML file.

Views > XML Code:

<record model="ir.ui.view" id="account_move_inherit_form_view">
   <field name="name">account.move.inherit.form.view</field>
   <field name="model">account.move</field>
   <field name="inherit_id" ref="account.view_move_form"/>
   <field name="arch" type="xml">
       <xpath expr="//header" position="inside">
           <button name="action_get_attachment" string="Generate Attachment" type="object" class="oe_highlight"/>
       </xpath>
   </field>
</record>

The corresponding function in the python file gets executed when the button click is enabled. The report reference id for the qweb-pdf report can be called inside a function. For that, we need the following code inside the report XML file.

Report > XML Code (Report):

<data>
   <!-- QWeb Reports -->
   <record id="report_id" model="ir.actions.report">
       <field name="name">Model Report</field>
       <field name="model">account.move</field>
       <field name="report_type">qweb-pdf</field>
       <field name="report_name">module_name.templates_id</field>
       <field name="report_file">module_name.templates_id</field>
   </record>
</data>

The template is needed to generate a pdf report. The record inside the report XML code is called to the template. So let’s define a report template file.

Inside the template also defines the contents of the report. We can change these contents according to our needs.

Report > XML Code (Template):

<template id="template_id">
   <t t-call="web.external_layout">
       <div class="page">
<!--   Document Content   -->
       </div>
   </t>
</template>
<template id="templates_id">
   <t t-call="web.html_container">
       <t t-foreach="docs" t-as="o">
           <t t-call="module_name.template_id"/>
       </t>
   </t>
</template>

The image below highlights the ‘Generate Attachment’ Button. When clicked on the button, it automatically generates a pdf attachment based on the report template.

how-to-create-pdf-attachment-automatically-in-odoo-15-cybrosys

The below image shows the generated pdf attachment in the chatter of the corresponding model.

how-to-create-pdf-attachment-automatically-in-odoo-15-cybrosys

Go to Settings > Technical > Database Structure > Attachments

Here, we can view all the created attachments.

how-to-create-pdf-attachment-automatically-in-odoo-15-cybrosys

The below image shows the corresponding record of generated attachment.

how-to-create-pdf-attachment-automatically-in-odoo-15-cybrosys

The feature of automatic generation attachment is helpful in the working of the company. This is the simple way to generate attachments using the button ‘action_get_attachment’ function.


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



0
Comments



Leave a comment

 


whatsapp
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message