【问题标题】:How to add new page to report from other PDF如何添加新页面以从其他 PDF 报告
【发布时间】:2020-05-25 09:05:20
【问题描述】:

我有一份报告 (qweb-pdf)。如何从另一个 pdf 向此报告添加新页面?

<template id="report_commercial_offer">
    <t t-call="web.html_container">
      <t t-foreach="docs"
        t-as="doc">
        ... some code
         <p style="page-break-after:always;">   </p>
          <p> Test New Page  </p>
          <div class="t">
                 <span t-esc="doc.test_func()" />
          </div>
      </t>
    </t>
  </template>

在这个函数test_func()我想从另一个pdf(例如D:\file1.pdf)添加一个页面到这个pdf。我尝试使用库:PyPDF2slate3k 但失败了...

【问题讨论】:

    标签: python pdf odoo-12


    【解决方案1】:

    您可以在下载报告之前使用控制器添加页面。

    from PyPDF2 import PdfFileReader, PdfFileWriter
    import io
    from odoo import http
    from odoo.http import request
    
    
    class MergePdf(http.Controller):
    
        @http.route('/report/custom_invoice/<int:invoice_id>', auth="user")
        def print_custom_invoice(self, invoice_id, **kw):
    
            report_date, report_name = request.env.ref('account.account_invoices').sudo().render_qweb_pdf([invoice_id])
            pdf_data = io.BytesIO(report_date)
            file1 = PdfFileReader(stream=pdf_data)
    
            # Store template PDF in ir.attachment table
            page = request.env['ir.attachment'].search([('name', '=', 'invoice_document')], limit=1)
            page_data = io.BytesIO(base64.b64decode(page.datas))
            file2 = PdfFileReader(stream=page_data)
    
            # Read a template PDF
            # file2 = PdfFileReader(open(file_path, "rb"))
    
            output = PdfFileWriter()
    
            # Add all report pages
            output.appendPagesFromReader(file1)
    
            # Add the requested page from template pdf
            output.addPage(file2.getPage(0))
    
            output_stream = io.BytesIO()
            output.write(output_stream)
    
            data = output_stream.getvalue()
            pdf_http_headers = [('Content-Type', 'application/pdf'), ('Content-Length', len(data))]
            return request.make_response(data, headers=pdf_http_headers)
    

    然后使用按钮打印报告:

    @api.multi
    def send_sms(self):
        self.ensure_one()
        return {
            "type": "ir.actions.act_url",
            "url": "/report/custom_invoice/%s" % self.id,
            "target": "self",
        }
    

    【讨论】:

    • Kenly 我不明白为什么会收到错误:500 内部服务器错误。这里:report_date,report_name = request.env.ref('vt_product_configurator.action_report_commercial_offer').sudo().render_qweb_pdf([invoice_id])
    • 你能检查日志文件中的错误描述吗?
    • 肯利,哦,对不起。我的报告 action_report_commercial_offer 有错误。
    • 肯利,还有一个问题。突然你知道了:stackoverflow.com/questions/61947456/…
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2019-02-21
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多