2

I have used this below code.

from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject


def set_need_appearances_writer(writer: PdfFileWriter):
    # See 12.7.2 and 7.7.2 for more information:
    # http://www.adobe.com/content/dam/acom/en/devnet/acrobat/
    #   pdfs/PDF32000_2008.pdf
    try:
        catalog = writer._root_object
        # get the AcroForm tree
        if "/AcroForm" not in catalog:
            writer._root_object.update(
                {
                    NameObject("/AcroForm"): IndirectObject(
                        len(writer._objects), 0, writer
                    )
                }
            )

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
        # del writer._root_object["/AcroForm"]['NeedAppearances']
        return writer

    except Exception as e:
        print("set_need_appearances_writer() catch : ", repr(e))
        return writer


list_data = [
    {"7014": "1", "Datframst": "2022-04-05"},
    {"7014": "2", "Datframst": "2022-04-05"},
]

myfile = PdfFileReader("template/test.pdf")

writer = PdfFileWriter()
set_need_appearances_writer(writer)


for dict_data in list_data:
    for count in range(myfile.numPages):
        writer.updatePageFormFieldValues(
            myfile.getPage(count),
            fields=dict_data
        )
        writer.addPage(myfile.getPage(count))


with open("newfile.pdf", "wb") as new:
    writer.write(new)

list_data in the for loop has multiple dict. What this code does is create a file with multiple pages which I want but all the pages gets overwriiten by the last dict value (dict_data). When I do this with just one dict data the file with two pages of myfile varible has no problem. But when I use multiple dicts then the problem arises of all pages having same values. Please Help!

You can get the file here.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
varun
  • 27
  • 7

1 Answers1

1

Think about pdf file as a one form. Not like one form per one page. In your case your last iteration overwrites whole file form. So you need to have unique field names in the whole file. It can be achieved by renaming fields like:

def _build_pages(self):
    for count, _ in enumerate(self._report_data):
        template_page = copy.deepcopy(self._template_page)
        self.pdf_suffix_fields(template_page, str(count))
        self._writer.add_page(template_page)

@staticmethod
def pdf_suffix_fields(page, sfx):
    for j in range(0, len(page['/Annots'])):
        writer_annot = page['/Annots'][j].get_object()
        try:
            writer_annot.update({
                NameObject("/T"): create_string_object(f"{writer_annot['/T']}_page{sfx}")
            })
        except KeyError:
            pass
Vit Amin
  • 574
  • 5
  • 20