(Odoo) 如何从网站下载 PDF 报告?

(Odoo) How to download PDF reports from website?

如何从网站上的控制器下载 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'

谢谢

您的代码的第 4 行似乎 [0] 对我不利。你能把它拿下来再试一次吗?例如:

@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'], 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)

我没有使用 ref,而是使用了方法 _get_report_from_name。