如何从其他PDF向报告添加新页面

2024-04-26 13:47:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份报告(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,但失败了


Tags: testreportdivwebiddocpdf报告
1条回答
网友
1楼 · 发布于 2024-04-26 13:47:28

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

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",
    }

相关问题 更多 >