Enable Dark Mode!
how-to-add-pdf-attachments-to-the-chatter-in-odoo-18.jpg
By: Ruksana P

How to Add PDF Attachments to the Chatter in Odoo 18

Technical Odoo 18 Odoo Enterprises Odoo Community

By using Odoo Chatter, users can manually upload files, including PDFs, directly to records. This is useful for attaching documents, such as invoices or contracts. You can simply drag and drop the PDF into the Chatter section of the desired record.

In this blog, we can discuss how to attach a PDF to the Chatter programmatically via a custom module. 

We can generate a PDF (e.g., a delivery slip or custom report) and attach it to the chatter of a record, such as a Sale Order, Invoice, or Manufacturing Order, automatically. There are 3 steps.

  • Generate the PDF File

Use Odoo's QWeb report rendering to generate your PDF.

pdf_content, _ =self.env['ir.actions.report'].sudo()._render_qweb_pdf(               'your_module_name.action_report, order.id)
  • Create the Attachment

Create a new record in ir. attachment and link it to the model/record.

self.env['ir.attachment'].create({    'name': 'My Report.pdf',    'type': 'binary',    'datas': base64.b64encode(pdf_content),    'res_model': record._name,    'res_id': record.id,    'mimetype': 'application/pdf',})
  • Post a Message in the Chatter

If you want to log a message in the chatter to show that a PDF has been added:

record.message_post(    body='The document has been attached.',    attachment_ids=[attachment.id])

Let’s create an example in sale.order and add the attachment option in confirm of sale order.

# -*- coding: utf-8 -*-from odoo import models, fieldsimport base64class SaleOrder(models.Model):   _inherit = 'sale.order'   def action_confirm(self):       """ Override the default action_confirm to attach a generated           PDF before confirming the record."""       self.action_attach_pdf()       return super().action_confirm()   def action_attach_pdf(self):       """       Generate and attach the Sale Order PDF to each record.       This method uses the specified QWeb report to generate a PDF           for each sale order record. It then creates an attachment       with the generated PDF, links it to the record, and posts a         message indicating the PDF has been attached.       The attachment is stored in the chatter (message thread) of        the record.       """       for order in self:           pdf,_ =              self.env['ir.actions.report'].sudo().report_template_id(               'sale.action_report_saleorder', order.id)           attachment = self.env['ir.attachment'].create({               'name': f'SaleOrder_{order.name}.pdf',               'type': 'binary',               'datas': base64.b64encode(pdf),               'res_model': order._name,               'res_id': order.id,               'mimetype': 'application/pdf',           })           order.message_post(body='Sale Order PDF attached.',             attachment_ids=[attachment.id])

This code generates a report of the sale order and attaches it to the chatter when the sale order is confirmed, as shown in below. We can create a new sales order report or use existing ones.

How to Add PDF Attachments to the Chatter in Odoo 18-cybrosys

Ensure the user has the correct access rights to post messages and create attachments.

The res_model and res_id fields ensure that the attachment is linked to the specific record.

We get the following report in the chatter.

How to Add PDF Attachments to the Chatter in Odoo 18-cybrosys

Integrating PDF attachments into Chatter in Odoo 18 enhances traceability and collaboration across teams. Whether you're uploading files manually or automating the process with code, Odoo provides flexible options to keep important documents tied to business records.

To read more about How to Add Chatter to Form View in Odoo 18, refer to our blog How to Add Chatter to Form View in Odoo 18.


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



0
Comments



Leave a comment



whatsapp_icon
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