Updated pdf fields don't show up when page is written - French CERFA document

49 Views Asked by At

I try again publishing this:

I need to fill forms from one formular to another pdf document:

link to the final pdf document: https://entreprendre.service-public.fr/vosdroits/R23301

here the function supposed to resolve the writting in the pdf file:

def set_need_appearances_writer(writer):
    # 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

and here the code to fill it:

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

# inputFile = "./PDF/DT-DICT-DEMANDE-print.pdf"

def change_PdfFile_2(inputFile):
    """
        Change outputFile Content
    """    
    print("changeFile2 content function\n\n")

    # checking the right fields
    pdf_fields = PyPDF2.PdfReader(inputFile).get_form_text_fields()
    print(pdf_fields)
    
    # creating reader and writer file
    # first getting a filled pdf file and next changing form values and saving in a writer file

    pdf_reader = PyPDF2.PdfReader(inputFile)
    pdf_writer = PyPDF2.PdfWriter()
    
    page = pdf_reader.pages[0]
    
    # new fields
    data_dict = {
            'Denomination': 'NouveauNom', 
            'ComplementAdresse': None, 
            'NoVoie': '123 RUE STE SIMONE', 
            'LieuditBP': None, 
            'CodePostal': '33000', 
            'Commune': 'BORDEAUX', 
            'Pays': 'FRANCE',
            }
    
    # wishing to resolve the blocking of the pdf formular ...
    set_need_appearances_writer(pdf_writer)
    
    
    pdf_writer.add_page(page)
    pdf_writer.update_page_form_field_values(pdf_writer.get_page(0), fields=data_dict)

    with open('exportRecipice.pdf','wb') as new:
        pdf_writer.write(new)
            
    
def set_need_appearances_writer(writer):
    # 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

0

There are 0 best solutions below