-
Notifications
You must be signed in to change notification settings - Fork 0
/
writepdf.py
77 lines (61 loc) · 2.24 KB
/
writepdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import pdfrw
from PyPDF2 import PdfFileWriter, PdfFileReader
TEMPLATE_PATH = 'postal_template.pdf'
OUTPUT_PATH = 'postal_output.pdf'
ENCRYPT_PATH = 'postal_encrypted.pdf'
ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
template_pdf = pdfrw.PdfReader(input_pdf_path)
annotations = template_pdf.pages[1][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():
print("Matched: ", key)
annotation.update(
pdfrw.PdfDict(V='{}'.format(data_dict[key]))
)
# Hack to change appearance so filled up text fields show in Preview
annotation.update(pdfrw.PdfDict(AP=''))
pdfrw.PdfWriter().write(output_pdf_path, template_pdf)
def encrypt(input_pdf, output_pdf, password):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(input_pdf)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
pdf_writer.encrypt(user_pwd=password, owner_pwd=None,
use_128bit=True)
with open(output_pdf, 'wb') as fh:
pdf_writer.write(fh)
data_dict = {
'surname': 'Manocha',
'firstname': 'Pranay',
'addr1': 'Flat 1',
'addr2': 'Gates Lane',
'addr3': 'Somewhere',
'postcode': 'SO15 1XY',
'phoneno': '07567000000',
'email':'[email protected]',
#'permanentpostal':'On',
'electiondate_dd':'12',
'electiondate_mm':'12',
'electiondate_yyyy':'2019',
'dob_dd':'01',
'dob_mm':'01',
'dob_yyyy':'2000',
'app_dd':'22',
'app_mm':'10',
'app_yyyy':'2019'
}
if __name__ == '__main__':
# This works fine but I cannot figure out how to set the 'permanentpostal' field
write_fillable_pdf(TEMPLATE_PATH, OUTPUT_PATH, data_dict)
# Encrypting the PDF deletes all form data and additionally makes the PDF uneditable
encrypt(OUTPUT_PATH,ENCRYPT_PATH,"password123!")