Skip to Content
Menu
This question has been flagged
2 Replies
7018 Views

Please help to get a python function to attach Qweb Report (PDF file) in a binary field 

v15c


report_file = fields.Many2one('ir.attachment', string='PDF')
def generate_report_file(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

Tried above method, but showing the error "AttributeError: 'ir.actions.report' object has no attribute 'render_qweb_pdf'"


Avatar
Discard

in odoo 15 method is: _render_qweb_pdf

Author Best Answer

Thank you.. Niyas Raphy

It was _render_qweb_pdf()


def generate_report_file(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
If anyone still has problems with this, id did it this way
pdf_content = fields.Binary(string='PDF generado')   
def action_report(self):       
report_template_id = self.env.ref('base_icg.action_report_op_ticket')._render_qweb_pdf(self.ids)        report_data = report_template_id[0]           
# Convertir los datos del informe a bytes       
report_bytes = base64.b64encode(report_data)               
# Crear el registro de ir.attachment       
attachment_values = {
​'name': "Informe.pdf",           
​'type': 'binary',           
​'datas': report_bytes,           
​ 'store_fname': "Informe.pdf",           
​'mimetype': 'application/x-pdf',       
​ }       
​attachment = self.env['ir.attachment'].create(attachment_values)             
  # Almacenar el ID del archivo adjunto en el campo pdf_content     
self.pdf_content = attachment.datas               
return True
Avatar
Discard
Related Posts Replies Views Activity
1
Aug 23
1585
5
Nov 23
8260
1
Feb 23
1745
2
Feb 23
1826
0
May 25
153