How can I download a pdf report (Odoo v14) from a controller on the website?
I tried solving the problem with the following code:
@http.route(['/my/projects/invoices/<int:invoice_id>/download'], type='http', auth="user", website=True)
def download_customer_project_report(self, invoice_id):
invoice = request.env.ref('account.report_invoice_with_payments').sudo()._render_qweb_pdf([invoice_id])[0]
pdf_http_headers = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
return request.make_response(invoice, headers=pdf_http_headers)
But i kept runnign into this error AttributeError: 'ir.ui.view' object has no attribute '_render_qweb_pdf'
.
Thanks
I managed to fix the issue with the following code:
@http.route(['/my/projects/invoices/<int:invoice_id>/download'], type='http', auth="user", website=True)
def download_pdf(self, invoice_id):
invoice = request.env['account.move'].sudo().search([('id', '=', invoice_id)], limit=1)
if not invoice or invoice.partner_id.id != request.env.user.partner_id.id:
return None
pdf, _ = request.env['ir.actions.report']._get_report_from_name(
'account.report_invoice').sudo()._render_qweb_pdf(
[int(invoice_id)])
pdf_http_headers = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf)),
('Content-Disposition', content_disposition('%s - Invoice.pdf' % (invoice.name)))]
return request.make_response(pdf, headers=pdf_http_headers)
Instead of ref, I used the method _get_report_from_name.