Skip to Content
Menu
This question has been flagged
1 Reply
2518 Views

I am trying to add a Qweb Report (PDF file) to a binary field. I used the below code but I am getting the error message, "AttributeError: 'ir.actions.report' object has no attribute 'render_qweb_pdf". I am using odoo version 15.


report_file = fields.Many2one('ir.attachment', string='PDF')


def report_file_generate(self):

    report_template_id = self.env.ref('id_card_by_fas.id_card_template_report').render_qweb_pdf(self.id​)

    data_record = base64.b64encode(report_template_id[0])

    ir_values = {

        'name': "ID Card",

        'type': 'binary',

        'datas': data_record,

        'store_fname': data_record,

        'mimetype': 'application/x-pdf',

    }

    data_id = self.env['ir.attachment'].create(ir_values)

    self.report_file = data_id.id

    return True

Avatar
Discard
Best Answer

Hi Ronaldo,

it seems like you're attempting to generate a PDF report using a Qweb template and then store it in a binary field. The error you're encountering suggests that the ir.actions.report object does not have a method called render_qweb_pdf. This is because the ir.actions.report model doesn't directly provide such a method.

Instead, you should use the render_qweb_pdf method available on the ir.actions.report model. Here's how you can modify your code to achieve this:

from io import BytesIO
import base64

def report_file_generate(self):
    # Fetch the report template
    report_template = self.env.ref('id_card_by_fas.id_card_template_report')

    # Render the report as PDF
    report_pdf, _ = report_template.render_qweb_pdf([self.id])

    # Convert PDF content to base64
    pdf_base64 = base64.b64encode(report_pdf)

    # Create an attachment record
    attachment_values = {
        'name': "ID_Card.pdf",
        'type': 'binary',
        'datas': pdf_base64,
        'mimetype': 'application/pdf',
    }
    attachment = self.env['ir.attachment'].create(attachment_values)

    # Assign the attachment to the binary field
    self.report_file = attachment.id

    return True



Avatar
Discard