Skip to content

Commit

Permalink
fix encoding and xrange issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yininge committed Dec 6, 2019
1 parent 2254600 commit f14c898
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions pdfjinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
if sys.version_info[0] == 3:
PY3 = True

xrange = range

try:
from logging import NullHandler
Expand Down Expand Up @@ -174,10 +175,7 @@ def parse_annotations(self, pgnum, page):
tmpl = ref["TU"]

try:
if ref["TU"].startswith(b"\xfe"):
tmpl = tmpl.decode("utf-16")
else:
tmpl = tmpl.decode("utf-8")
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)
Expand Down Expand Up @@ -234,7 +232,7 @@ def __call__(self, data, attachments=[], pages=None):
# Skip the field if it is already rendered by filter
if field not in self.rendered:
if PY3:
field = field.decode('utf-8')
field = field.decode('latin1')
self.rendered[field] = rendered_field

filled = PdfFileReader(self.exec_pdftk(self.rendered))
Expand All @@ -243,7 +241,7 @@ def __call__(self, data, attachments=[], pages=None):
page.mergePage(watermark)

output = PdfFileWriter()
pages = pages or range(filled.getNumPages())
pages = pages or xrange(filled.getNumPages())
for p in pages:
output.addPage(filled.getPage(p))

Expand Down

0 comments on commit f14c898

Please sign in to comment.