-
Notifications
You must be signed in to change notification settings - Fork 4
/
filler.py
377 lines (303 loc) · 11.6 KB
/
filler.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import json
from pprint import pprint
import argparse
from math import sin, cos, pi
import sys
# An attempt at Python2/Python3 compat.
try:
from StringIO import StringIO as CharIO
except ImportError:
from io import BytesIO as CharIO
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen import canvas
LEFT = 'left'
RIGHT = 'right'
CENTER = 'center'
PREVIEW_COLOR = (0x20, 0xF0, 0x90,)
class FormRenderer(object):
"""
Render JSON defined fields on PDF document.
"""
def __init__(self, base_form, form_data_file, output_file,
extra_data_file=None, preview=None):
# XXX: We don't validate these files...
self.base_form = base_form
self.output_file = output_file
self.preview = preview
with open(form_data_file) as f:
form_data = json.load(f)
if extra_data_file:
with open(extra_data_file) as f:
extra_data = json.load(f)
self.fields = [x for x in form_data]
if extra_data_file:
self.fields += extra_data
self.fields.sort(key=lambda x: x['page'], reverse=False)
def render(self):
"""
Render text on PDF document.
"""
with open(self.base_form, 'rb') as f:
self.docbuf = CharIO(f.read())
self.overlaybuf = CharIO()
self.filledbuf = CharIO()
self.form = PdfFileReader(self.docbuf, strict=False) #.getPage(0)
self.pages = self.form.getNumPages()
self.pagesize = self.form.getPage(0).mediaBox.upperRight
self.overlay = canvas.Canvas(self.overlaybuf, pagesize=self.pagesize)
fields = [x for x in self.fields]
for page_num in range(1, self.pages+1):
for i, field in enumerate(fields):
# Do not consider fields that belong to subsequent pages.
if field['page'] > page_num or field['page'] < page_num:
break
# Render field on current canvas page.
self.render_field(field)
# Remove fields that have already been placed on page.
fields = fields[i:]
# Next page
self.overlay.showPage()
self.overlay.save()
self.overlaybuf.seek(0)
self.final = PdfFileReader(self.overlaybuf, strict=False)
self.output = PdfFileWriter()
# Merge text overlay pages onto original document pages.
for i in range(self.pages):
page = self.form.getPage(i)
page.mergePage(self.final.getPage(i))
self.output.addPage(page)
# Write to buffer and write to file.
self.output.write(self.filledbuf)
self.write_to_file(self.output_file)
def write_to_file(self, filename):
"""
Write the completed PDF to file.
"""
# XXX No error checkinng on the write...
output_file = open(filename, 'wb')
output_file.write(self.filledbuf.getvalue())
output_file.close()
def render_field(self, field):
"""
Render text or image field.
"""
if self.preview:
self.render_preview_box(field)
if field.get('type', 'text') == "image":
draw_point = self.calculate_image_draw_point(field)
draw_fnct = self.render_image
if (field.get('type', 'text') == "text"):
draw_point = self.calculate_text_draw_point(field)
draw_fnct = self.render_text
if (field.get('type', 'text') == "outline"):
draw_point = self.calculate_image_draw_point(field)
draw_fnct = self.render_outline
if (field.get('type', 'text') == "line"):
draw_point = self.calculate_image_draw_point(field)
draw_fnct = self.render_line
draw_fnct(field, draw_point)
def render_line(self, field, draw_point):
"""
Render outline rectangle with hard-wired radius.
"""
c = self.overlay # Canvas
x, y = draw_point
_, _, width, height = self.get_position_and_size(field)
x2 = x + width
y2 = y + height
c.saveState()
c.translate(x, y)
if 'rotation' in field:
c.rotate(int(field['rotation']))
if 'line_width' in field:
c.setLineWidth(int(field['line_width']))
(r, g, b) = self.calculate_rgb_values(field)
c.setStrokeColorRGB(r, g, b)
c.line(0, 0, width, height)
c.restoreState()
def render_outline(self, field, draw_point):
"""
Render outline rectangle with hard-wired radius.
"""
c = self.overlay # Canvas
x, y = draw_point
_, _, width, height = self.get_position_and_size(field)
radius = 5 # Hard-wired radius.
c.saveState()
c.translate(x, y)
if 'rotation' in field:
c.rotate(int(field['rotation']))
if 'line_width' in field:
c.setLineWidth(int(field['line_width']))
(r, g, b) = self.calculate_rgb_values(field)
c.setStrokeColorRGB(r, g, b)
c.roundRect(0, 0, width, height, radius, stroke=True, fill=False)
c.restoreState()
def render_image(self, field, draw_point):
"""
Render image field.
"""
c = self.overlay # Canvas
x, y = draw_point
_, _, width, height = self.get_position_and_size(field)
# TODO: validate file?
img_file = field['data']
c.saveState()
c.translate(x, y)
if 'rotation' in field:
c.rotate(int(field['rotation']))
c.drawImage(img_file, 0, 0, width, height, preserveAspectRatio=True)
c.restoreState()
def render_text(self, field, draw_point):
"""
Resolve the field value and draw left, right, or center.
"""
c = self.overlay # Canvas
x, y = draw_point
# Support 'data' and 'text' attributes. Preference for 'data'.
if 'data' in field:
field_value = field['data']
else:
field_value = field['text']
# Shrink text by 1 character until it fits.
# XXX: Danger, this will result if a single char won't fit.
while (pdfmetrics.stringWidth(field_value, field['font_face'],
field['font_size']) > float(field['width'])):
if (len(field_value) > 1):
field_value = field_value[:len(field_value)-1]
if (len(field_value) == 1):
raise Exception("Single character won't fit. You should "
"fix that. Stubbornly refusing to continue")
c.saveState()
c.translate(x, y)
if 'rotation' in field:
c.rotate(int(field['rotation']))
(r, g, b) = self.calculate_rgb_values(field)
c.setFillColorRGB(r, g, b)
if field['align_horizontal'] == LEFT:
c.drawString(0, 0, "{}".format(field_value))
if field['align_horizontal'] == RIGHT:
c.drawRightString(0, 0, "{}".format(field_value))
if field['align_horizontal'] == CENTER:
c.drawCentredString(0, 0, "{}".format(field_value))
c.restoreState()
def render_preview_box(self, field):
"""
Render the preview box.
"""
c = self.overlay # Canvas
# (x, y), field width, field height
x, y, width, height = self.get_position_and_size(field)
red, green, blue = PREVIEW_COLOR
red = float(red) / 255
green = float(green) / 255
blue = float(blue) / 255
c.saveState()
c.translate(x, y)
c.setFillColorRGB(red, green, blue)
c.setStrokeColorRGB(0, 0, 0)
c.setLineWidth(0.5)
if 'rotation' in field:
c.rotate(int(field['rotation']))
c.rect(0, 0, width, height, fill=1)
c.restoreState()
def get_position_and_size(self, field):
"""
Extract position, field width, and field height. Return as tuple.
"""
# These are all required. If not present, don't handle error.
x, y = float(field['x']), float(field['y'])
w, h = float(field['width']), float(field['height'])
return (x, y, w, h,)
def calculate_image_draw_point(self, field):
"""
Calculate correct coordinates to draw image.
"""
x = float(field['x'])
y = float(field['y'])
return (x, y)
def calculate_text_draw_point(self, field):
"""
Calculate correct coordinates to draw text.
"""
c = self.overlay # Canvas
x = float(field['x'])
y = float(field['y'])
field_height = float(field['height'])
# Always vertically center text within field.
c.setFont(field['font_face'], int(field['font_size']))
font_face = pdfmetrics.getFont(field['font_face']).face
font_size = float(field['font_size'])
ascent = (font_face.ascent * font_size) / 1000.0
voffset = (field_height - ascent) / 2
# Radians
rot = 0
if 'rotation' in field:
rot += float(field['rotation']) * pi / 180
xoffset = voffset * sin(rot)
yoffset = voffset * cos(rot)
if field['align_horizontal'] == LEFT:
x -= xoffset
y += yoffset
if field['align_horizontal'] == RIGHT:
x += float(field['width']) * cos(rot) - xoffset
y += float(field['width']) * sin(rot) + yoffset
if field['align_horizontal'] == CENTER:
x += (float(field['width']) / 2) * cos(rot) - xoffset
y += (float(field['width']) / 2) * sin(rot) + yoffset
return (x, y)
def calculate_rgb_values(self, field):
"""
Calculate R, G, B values from hexadecimal color value if present.
Default to black.
"""
(r, g, b,) = (0, 0, 0,)
if 'font_color' in field:
color = field['font_color']
if len(color) != 6:
raise Exception("Requires hex RGB colors in format 112233.")
r = int("0x{}".format("".join(list(color)[:2])), 16) / 255.0
g = int("0x{}".format("".join(list(color)[2:4])), 16) / 255.0
b = int("0x{}".format("".join(list(color)[4:6])), 16) / 255.0
return (r, g, b,)
class FormArgumentParser(argparse.ArgumentParser):
"""
Custom argparser.
"""
def error(self, message):
sys.stderr.write('error: {}\n'.format(message))
self.print_help()
sys.exit(2)
def usage_message(parser):
"""
Print a message and exit.
"""
sys.stderr.write("error: Missing required arguments.\n")
parser.print_help()
sys.exit(3)
def main():
"""
Parse command-line arguments. Process form.
"""
parser = FormArgumentParser()
parser.add_argument("-f", "--base-form",
help="The form to which the data will be applied.")
parser.add_argument("-d", "--form-data",
help="Data to be applied to the form.")
parser.add_argument("-e", "--extra-data",
help="Extra data to be applied to the form.")
parser.add_argument("-o", "--output-file",
help="Write completed form to this file.")
parser.add_argument("-p", "--preview", action='store_true',
help="Background coloring to help position fields.")
args = parser.parse_args()
# Let extra-data be optional. The remainder cannot be optional.
for arg in [args.base_form, args.form_data, args.output_file]:
if not arg:
usage_message(parser)
renderer = FormRenderer(args.base_form, args.form_data, args.output_file,
args.extra_data, args.preview)
renderer.render()
if __name__ == "__main__": # pragma: no cover
main()