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.
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.
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.