0

I'm facing the following problem when I add the following AcroForm option template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))

The text will show fine on the form but the checkmark does not show, the following image shows an example. Image of the form result with NeedAppearance set to true

But if I take out the option itself the checkmark shows completely fine but the texts do not.

The following image is an example

I understand this to be completely normal since NeedAppearances needs to be enabled to show the text in the text box. But I do not understand why the option would change the visibility of the checkbox.

It is my personal opinion that it has to do with the formatting of the PDF form but I'm not sure. Image of the form result without the option

The code

import pdfrw
from datetime import date

TEMPLATE_PATH = 'sample-form.pdf'
OUTPUT_PATH = 'starter_form.pdf'

ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'


def fill_pdf(input_pdf_path, output_pdf_path, data_dict):
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) 
    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=''))
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)


def fill_starter_checklist(data):
    today = date.today()
    data_dict = {
        'field_nrt': data['nrt_persona'],
        'field_raoSocial': data['rao_social'],
        'check_periodeLiquidacioJuliol': True if data['question_9'] == True else False,
        '0': data['numZero'],
    }

    return fill_pdf(TEMPLATE_PATH, OUTPUT_PATH, data_dict)


starter_checklist = {
    'nrt_persona': 'This is NRT',
    'rao_social': 'TestString',
    'question_9': True,
    'numZero':'2',

}

if __name__ == '__main__':
    fill_starter_checklist(starter_checklist)

If you want to download the form it can be downloaded from the following link Government forms

You will have to click the IGI tab and then click the PDF icon of form 910.

Government form page

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • @KJ I think that your idea is really solid, but I have no idea how to export the PDF to an FDF file, could you please recommend to me the tools you would use for the job? – Ernest Molner Feb 18 '22 at 08:21
  • Fond a tool, my friend thanks a lot @KJ , I'm using `pdftk`, is it good? It did the job for me, but if you think there are better tools out I would love a recommendation – Ernest Molner Feb 18 '22 at 09:50
  • 1
    Sorry for the delayed response @KJ firstly thanks a lot for the recommendation, right now I don't have time to try it out, but when I have some spare time ill test it extensively and post my conclusions on this thread – Ernest Molner Feb 24 '22 at 19:09
  • Instead of filling just the AS, have you tried filling both AS and V? `val_str = pdfrw.objects.pdfname.BasePdfName('/On') annotation.update(pdfrw.PdfDict(AS=val_str,V=val_str))` (based on https://westhealth.github.io/exploring-fillable-forms-with-pdfrw.html) – MoogleLogic Feb 02 '23 at 22:25

0 Answers0