-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfjinja.py
298 lines (235 loc) · 8.76 KB
/
pdfjinja.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
# -*- coding: utf-8 -*-
""" Use jinja2 templates to fill and sign PDF forms. """
import argparse
import datetime
import logging
import sys
import os
import time
from fdfgen import forge_fdf
from jinja2 import Environment, TemplateSyntaxError
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.pdftypes import PDFObjRef
from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
from PIL import Image, ImageDraw, ImageFont
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
from subprocess import Popen, PIPE
try:
from cStringIO import StringIO as BytesIO
except ImportError:
try:
from StringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
PY3 = False
if sys.version_info[0] == 3:
PY3 = True
xrange = range
try:
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass
logger = logging.getLogger(__name__)
logger.addHandler(NullHandler())
class Attachment(object):
label_x = 8
label_y = 8
font = None
fontsize = 12
def __init__(self, data, dimensions=None, text=None, font=None):
img = Image.open(data)
self.img = img
self.dimensions = dimensions or (0, 0, img.size[0], img.size[1])
if img.mode == "RGBA":
self.img = Image.new("RGB", img.size, (255, 255, 255))
mask = img.split()[-1] # 3 is the alpha channel
self.img.paste(img, mask=mask)
if font is not None:
self.font = font
if text is not None:
font = ImageFont.truetype(self.font, self.fontsize)
lines = text.split(os.linesep)
dims = [font.getsize(l) for l in lines]
w = sum(w for w, h in dims)
h = sum(h for w, h in dims)
self.label = Image.new("RGB", (w, h), (255, 255, 255))
draw = ImageDraw.Draw(self.label)
y = 0
for (lw, lh), line in zip(dims, lines):
draw.text((0, y), line, (0, 0, 0), font=font)
y += lh
def pdf(self):
stream = BytesIO()
pdf = canvas.Canvas(stream)
w, h = self.img.size
pdf.drawImage(ImageReader(self.img), *self.dimensions)
if hasattr(self, "label"):
w, h = self.label.size
x, y = self.label_x, self.label_y
pdf.drawImage(ImageReader(self.label), x, y, w, h)
pdf.save()
return PdfFileReader(stream).getPage(0)
class PdfJinja(object):
Attachment = Attachment
def __init__(self, filename, jinja_env=None):
self.jinja_env = Environment()
self.context = None
self.fields = {}
self.watermarks = []
self.filename = filename
self.register_filters()
with open(filename, "rb") as fp:
self.parse_pdf(fp)
def register_filters(self):
self.jinja_env.filters.update(dict(
date=self.format_date,
paste=self.paste,
check=lambda v: "Yes" if v else "Off",
X=lambda v: "X" if v else " ",
Y=lambda v: "Y" if v else "N",
))
def paste(self, data):
rect = self.context["rect"]
x, y = rect[0], rect[1]
w, h = rect[2] - x, rect[3] - y
pdf = self.Attachment(data, dimensions=(x, y, w, h)).pdf()
self.watermarks.append((self.context["page"], pdf))
return " "
def parse_pdf(self, fp):
parser = PDFParser(fp)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
device = PDFDevice(rsrcmgr)
interpreter = PDFPageInterpreter(rsrcmgr, device)
for pgnum, page in enumerate(PDFPage.create_pages(doc)):
interpreter.process_page(page)
page.annots and self.parse_annotations(pgnum, page)
def parse_annotations(self, pgnum, page):
annots = page.annots
if isinstance(page.annots, PDFObjRef):
annots = page.annots.resolve()
annots = (
r.resolve() for r in annots if isinstance(r, PDFObjRef))
widgets = (
r for r in annots if r["Subtype"].name == "Widget")
for ref in widgets:
data_holder = ref
try:
name = ref["T"]
except KeyError:
ref = ref['Parent'].resolve()
name = ref['T']
field = self.fields.setdefault(name, {"name": name, "page": pgnum})
allowed_fts = ("Btn", "Tx", "Ch", "Sig")
if "FT" in data_holder and data_holder["FT"].name in allowed_fts:
field["rect"] = data_holder["Rect"]
if "TU" in ref:
tmpl = ref["TU"]
try:
tmpl = tmpl.decode("latin1")
field["template"] = self.jinja_env.from_string(tmpl)
except (UnicodeDecodeError, TemplateSyntaxError) as err:
logger.error("%s: %s %s", name, tmpl, err)
def template_args(self, data):
kwargs = {}
today = datetime.datetime.today().strftime("%m/%d/%y")
kwargs.update({"today": today})
kwargs.update(data)
return kwargs
def format_date(self, datestr):
if not datestr:
return ""
ts = time.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%fZ")
dt = datetime.datetime.fromtimestamp(time.mktime(ts))
return dt.strftime("%m/%d/%y")
def exec_pdftk(self, data):
fdf_kwargs = dict(checkbox_checked_name="Yes")
fdf = forge_fdf("", data.items(), [], [], [], **fdf_kwargs)
args = [
"pdftk",
self.filename,
"fill_form", "-",
"output", "-",
"dont_ask",
"flatten"
]
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(fdf)
if stderr.strip():
raise IOError(stderr)
return BytesIO(stdout)
def __call__(self, data, attachments=[], pages=None):
self.rendered = {}
for field, ctx in self.fields.items():
if "template" not in ctx:
continue
self.context = ctx
kwargs = self.template_args(data)
template = self.context["template"]
try:
rendered_field = template.render(**kwargs)
except Exception as err:
logger.error("%s: %s %s", field, template, err)
else:
# Skip the field if it is already rendered by filter
if field not in self.rendered:
if PY3:
field = field.decode('latin1')
self.rendered[field] = rendered_field
filled = PdfFileReader(self.exec_pdftk(self.rendered))
for pagenumber, watermark in self.watermarks:
page = filled.getPage(pagenumber)
page.mergePage(watermark)
output = PdfFileWriter()
pages = pages or xrange(filled.getNumPages())
for p in pages:
output.addPage(filled.getPage(p))
for attachment in attachments:
output.addBlankPage().mergePage(attachment.pdf())
return output
def parse_args(description):
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"-f", "--font", type=str, default=None,
help="TTF font for attachment labels.")
parser.add_argument(
"-j", "--json", type=argparse.FileType("rb"), default=sys.stdin,
help="JSON format file with data.")
parser.add_argument(
"-p", "--page", type=str, default=None,
help="Pages to select (Comma separated).")
parser.add_argument(
"pdf", type=str,
help="PDF form with jinja2 tooltips.")
parser.add_argument(
"out", nargs="?", type=argparse.FileType("wb"), default=sys.stdout,
help="PDF filled with the form data.")
return parser.parse_args()
def main():
logging.basicConfig()
args = parse_args(__doc__)
pdfparser = PdfJinja(args.pdf)
pages = args.page and args.page.split(",")
import json
json_data = args.json.read().decode('utf-8')
data = json.loads(json_data)
Attachment.font = args.font
attachments = [
Attachment(**kwargs) for kwargs in data.pop("attachments", [])
]
output = pdfparser(data, attachments, pages)
output.write(args.out)
if __name__ == "__main__":
main()