问题描述
如何从网站上的控制器下载 pdf 报告 (odoo v14)?
@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)
但我一直遇到这个错误 AttributeError: 'ir.ui.view' object has no attribute '_render_qweb_pdf'
。
谢谢
解决方法
似乎 [0] 在您的代码的第 4 行对我不利。你能不能把它去掉然后再试一次。例如:
@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])
pdf_http_headers = [('Content-Type','application/pdf'),('Content-Length',len(pdf))]
return request.make_response(invoice,headers=pdf_http_headers)
,
我设法用以下代码解决了这个问题:
@http.route(['/my/projects/invoices/<int:invoice_id>/download'],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',len(pdf)),('Content-Disposition',content_disposition('%s - Invoice.pdf' % (invoice.name)))]
return request.make_response(pdf,headers=pdf_http_headers)
我没有使用 ref,而是使用了 _get_report_from_name 方法。