After using PDFRW in Python to update fields - triggering an update?

27 Views Asked by At

I am currently using python 3.10 and the PDFRW library to edit PDF files. At this point, inputting the data is good, turning on/off checkboxes is good, no issues with data input via Python - however there are some dependent fields that update based on some user input - but I can only get those fields to update after the file has been opened and some field updated manually.

I can extract all key/value/types from the PDF, I can open/save to different locations, I can update checkboxes and user-editable forms without difficulty. However, getting the secondary fields that are dependent on user input is throwing me off.

I have tried several methods to update this automatically after inputting the data, but this only results in the forms showing the default values when the file is opened. Again, when I manually open the file in a reader like Acrobat or Google Chrome then change some field like a checkbox, then all the fields update the way they should; but I am trying to get that update to occur before I even open the file.

def fill_pdf(input_pdf_path, output_pdf_path, data_dict):                                   
    template_pdf = pdfrw.PdfReader(input_pdf_path)                              
    for page in template_pdf.pages:                             
        annotations = page[ANNOT_KEY]                           
        for annotation in annotations:                          
            if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:                       
                if annotation[ANNOT_FIELD_KEY]:                 
                    key = annotation[ANNOT_FIELD_KEY][1:-1]             
                    if key in data_dict.keys():             
                        if type(data_dict[key]) == bool:            
                            if data_dict[key] == True:      
                                annotation.update(pdfrw.PdfDict(    
                                    AS=pdfrw.PdfName('Yes')))
                        else:           
                            annotation.update(      
                                pdfrw.PdfDict(V='{}'.format(data_dict[key]))    
                            )       
                            annotation.update(pdfrw.PdfDict(AP=''))     
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))                               
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(Fields=template_pdf.pages[0][ANNOT_KEY]))                               
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)                              

This is the code I use when updating the PDF, it does work, the fields I specify update without issue, from what I understand the following lines of code are supposed to update the fields:

template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))

template_pdf.Root.AcroForm.update(pdfrw.PdfDict(Fields=template_pdf.pages[0][ANNOT_KEY]))

but instead of updating based on the data I input, it instead updates the fields to their initial values. Am I doing something wrong?

0

There are 0 best solutions below