Python代码生成pdf报表 - xiaohao0576/odoo-doc GitHub Wiki
pdf_content, _ = env['ir.actions.report']._render_qweb_pdf('sale.action_report_pro_forma_invoice', order.id)
pdf_content_b64 = b64encode(pdf_content) # 服务器动作中默认导入了b64encode
attachment = env['ir.attachment'].create({
'name': f'业务受理单- {order.name}',
'type': 'binary',
'datas': pdf_content_b64, # 也可以使用raw字段,直接用pdf_content赋值,不需要base64编码
'res_model': 'sale.order',
'res_id': order.id,
'mimetype': 'application/pdf',
})
order.message_post(body='业务受理单已生成.',attachment_ids=[attachment.id])
注意事项
- _render_qweb_pdf函数执行的时候会自动创建attachment
- _render_qweb_pdf第一个参数是report_ref, 这个参数可以是报表名称,或者数据库ID
- _render_qweb_pdf第二个参数是res_ids, 这个参数是数据库ID列表
- pdf_content_b64可以借助webhook服务器动作,发送到外部服务器处理
源码参考
https://github.com/odoo/odoo/blob/9b62a312d5a966aa74099d79595689ad5d5496b8/odoo/addons/base/models/ir_actions_report.py#L1005