Reputation: 19
I want to save my pdf report as attachment so I can make a zip file of some attachments from that 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,
'datas_fname': name + '.pdf',
'store_fname': name,
'res_model': self._name,
'res_id': self.id,
'mimetype': 'application/x-pdf'
})
I get this solution for odoo version 13.0 from HERE, but it doesn't work so well in odoo version 14.0. It says that
Odoo Server Error
Traceback (most recent call last):
File "/home/admin/odoo/odoo-14/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
result = request.dispatch()
File "/home/admin/odoo/odoo-14/odoo/http.py", line 683, in dispatch
result = self._call_function(**self.params)
File "/home/admin/odoo/odoo-14/odoo/http.py", line 359, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/admin/odoo/odoo-14/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/home/admin/odoo/odoo-14/odoo/http.py", line 347, in checked_call
result = self.endpoint(*a, **kw)
File "/home/admin/odoo/odoo-14/odoo/http.py", line 912, in __call__
return self.method(*args, **kw)
File "/home/admin/odoo/odoo-14/odoo/http.py", line 531, in response_wrap
response = f(*args, **kw)
File "/home/admin/odoo/odoo-14/addons/web/controllers/main.py", line 1733, in run
result = action.run()
File "/home/admin/odoo/odoo-14/odoo/addons/base/models/ir_actions.py", line 629, in run
res = runner(run_self, eval_context=eval_context)
File "/home/admin/odoo/odoo-14/addons/website/models/ir_actions.py", line 61, in _run_action_code_multi
res = super(ServerAction, self)._run_action_code_multi(eval_context)
File "/home/admin/odoo/odoo-14/odoo/addons/base/models/ir_actions.py", line 498, in _run_action_code_multi
safe_eval(self.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'
File "/home/admin/odoo/odoo-14/odoo/tools/safe_eval.py", line 346, in safe_eval
raise ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr))
Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/admin/odoo/odoo-14/odoo/http.py", line 639, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/admin/odoo/odoo-14/odoo/http.py", line 315, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: <class 'AttributeError'>: "'ir.actions.report' object has no attribute 'render_qweb_pdf'" while evaluating
"action = env['wgs.form.1721.a1.list'].generate_attachments(env.context.get('active_ids'))"
I've tried similar solution from different sites, but it produces similar result to this one. Pls help. I think this is new in odoo-14. I'm new in odoo development as well, so I don't know that much
Upvotes: 2
Views: 4152
Reputation: 11
def action_get_attachment(self):
pdf = self.env.ref('project2.tr_cetificate_action_id')._render_qweb_pdf(self.ids)
b64_pdf = base64.b64encode(pdf[0])
name = "My Attachment"
return self.env['ir.attachment'].create({
'name': name,
'type': 'binary',
'datas': b64_pdf,
# 'datas_fname': name + '.pdf',
'store_fname': name,
'res_model': self._name,
'res_id': self.id,
'mimetype': 'application/x-pdf'
})
This code will work in version 14.
Upvotes: 1
Reputation: 11
pdf = self.env.ref('module_name..report_id')._render_qweb_pdf(self.ids)
You have to put _render_qweb_pdf like this.
Upvotes: 1