pikepdf添加页面会导致显示错误

时间:2020-07-13 17:19:48

标签: python pdf pdf-generation pikepdf

我有两页可填写的PDF,我正在用pikepdf填写。第二页包含当数据从第一页溢出时要填充的字段。有时,数据从第二页溢出,我想将第二页复制到第三页并填写第三页。由于复制页面不会创建新的AcroForm对象(它保留了第二页的结构),因此我想到了以下代码,该代码通过将重复的可填充页面附加到新的PDF中来正确添加重复的可填充页面。但是,这种技术会导致在Edge,Chrome,Drive PDF Viewer和Adobe Acrobat中查看PDF时显示的内容有所不同。在Edge and Drive PDF Viewer中,第三页显示与之关联的数据。在Chrome中,第三个显示为第二个页面的副本。在Adobe Acrobat中,任何字段均不显示任何值。

for i, row_info in enumerate(rows):
        if i >= 49 and (i-20)%29 == 0:

            pdf.root.AcroForm['/NeedAppearances'] = True

            for page in pdf.pages:
                if Name.Annots not in page:
                    continue
                annots = page.Annots
                for annot in annots:
                    if annot.Type != Name.Annot:
                        continue
                    if annot.Subtype == Name.Widget:
                        if Name.T in annot:
                            annot.Ff=1
            if (i == 49): #only append the first page once
                pdf_out.pages.append(pdf.pages[0])
            pdf_out.pages.append(pdf.pages[1])
            pdf.close()
            pdf = pikepdf.open(directory + "/my_pdf.pdf")
        
        fill_row(pdf, i, row_info)

    pdf.root.AcroForm['/NeedAppearances'] = True

    for page in pdf.pages:
        if Name.Annots not in page:
            continue
        annots = page.Annots
        for annot in annots:
            if annot.Type != Name.Annot:
                continue
            if annot.Subtype == Name.Widget:
                if Name.T in annot:
                    annot.Ff=1

    pdf_out.pages.append(pdf.pages[1])

我已经尝试在pdf_out.pages上同时使用.append和.extend来添加第三页。

fill_row(...)方法的工作原理如下。

def fill_row(...):
    ...
    fields = pdf.root.AcroForm.Fields[0].Kids[0].Kids
    fields[row_no].V = str(row_info[0])
    fields[row_no + 20].V = str(row_info[1])
    ...

在使用.append或.extend之后,有人知道如何在复制acroform时附加页面或如何解决跨不同查看器的渲染/显示问题吗?

0 个答案:

没有答案