issue #2959: improve Info setting and dumping

This commit is contained in:
Dvořák Václav 2018-01-26 12:11:10 +01:00
parent f9566877f3
commit 4d3b13fb08
3 changed files with 18 additions and 37 deletions

View File

@ -137,13 +137,13 @@ class TestFilePdf(PillowTestCase):
pdf = pdfParser.PdfParser(pdf_filename) pdf = pdfParser.PdfParser(pdf_filename)
self.assertEqual(len(pdf.pages), 1) self.assertEqual(len(pdf.pages), 1)
self.assertEqual(len(pdf.info), 1) self.assertEqual(len(pdf.info), 1)
self.assertEqual(pdfParser.decode_text(pdf.info[b"Producer"]), "pdfParser") self.assertEqual(pdfParser.decode_text(pdf.info["Producer"]), "pdfParser")
# append some info # append some info
pdf.info[b"Title"] = pdfParser.encode_text("abc") pdf.info["Title"] = "abc"
pdf.info[b"Author"] = pdfParser.encode_text("def") pdf.info["Author"] = "def"
pdf.info[b"Subject"] = pdfParser.encode_text("ghi") pdf.info["Subject"] = "ghi"
pdf.info[b"Keywords"] = pdfParser.encode_text("jkl") pdf.info["Keywords"] = "jkl"
pdf.info[b"Creator"] = pdfParser.encode_text("hopper()") pdf.info["Creator"] = "hopper()"
with open(pdf_filename, "r+b") as f: with open(pdf_filename, "r+b") as f:
f.seek(0, os.SEEK_END) f.seek(0, os.SEEK_END)
pdf.write_xref_and_trailer(f) pdf.write_xref_and_trailer(f)
@ -151,7 +151,7 @@ class TestFilePdf(PillowTestCase):
pdf = pdfParser.PdfParser(pdf_filename) pdf = pdfParser.PdfParser(pdf_filename)
self.assertEqual(len(pdf.pages), 1) self.assertEqual(len(pdf.pages), 1)
self.assertEqual(len(pdf.info), 6) self.assertEqual(len(pdf.info), 6)
self.assertEqual(pdfParser.decode_text(pdf.info[b"Title"]), "abc") self.assertEqual(pdfParser.decode_text(pdf.info["Title"]), "abc")
# append two images # append two images
mode_CMYK = hopper("CMYK") mode_CMYK = hopper("CMYK")
mode_P = hopper("P") mode_P = hopper("P")
@ -160,8 +160,8 @@ class TestFilePdf(PillowTestCase):
pdf = pdfParser.PdfParser(pdf_filename) pdf = pdfParser.PdfParser(pdf_filename)
self.assertEqual(len(pdf.pages), 3) self.assertEqual(len(pdf.pages), 3)
self.assertEqual(len(pdf.info), 6) self.assertEqual(len(pdf.info), 6)
self.assertEqual(pdfParser.decode_text(pdf.info[b"Title"]), "abc") self.assertEqual(pdfParser.decode_text(pdf.info["Title"]), "abc")
self.assertEqual(pdfParser.decode_text(pdf.info[b"Producer"]), "pdfParser") self.assertEqual(pdfParser.decode_text(pdf.info["Producer"]), "pdfParser")
def test_pdf_append_to_bytesio(self): def test_pdf_append_to_bytesio(self):
im = hopper("RGB") im = hopper("RGB")

View File

@ -62,17 +62,17 @@ def _save(im, fp, filename, save_all=False):
existing_pdf = pdfParser.PdfParser() existing_pdf = pdfParser.PdfParser()
if title: if title:
existing_pdf.info[b"Title"] = pdfParser.encode_text(title) existing_pdf.info["Title"] = title
if author: if author:
existing_pdf.info[b"Author"] = pdfParser.encode_text(author) existing_pdf.info["Author"] = author
if subject: if subject:
existing_pdf.info[b"Subject"] = pdfParser.encode_text(subject) existing_pdf.info["Subject"] = subject
if keywords: if keywords:
existing_pdf.info[b"Keywords"] = pdfParser.encode_text(keywords) existing_pdf.info["Keywords"] = keywords
if creator: if creator:
existing_pdf.info[b"Creator"] = pdfParser.encode_text(creator) existing_pdf.info["Creator"] = creator
if producer: if producer:
existing_pdf.info[b"Producer"] = pdfParser.encode_text(producer) existing_pdf.info["Producer"] = producer
# #
# make sure image data is available # make sure image data is available

View File

@ -221,24 +221,10 @@ class PdfArray(list):
class PdfDict(UserDict): class PdfDict(UserDict):
#def __init__(self, *args, orig_ref=None, pdf=None, **kwargs):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
UserDict.__init__(self, *args, **kwargs) UserDict.__init__(self, *args, **kwargs)
#self.orig_ref = kwargs.pop("orig_ref", None)
#self.pdf = kwargs.pop("pdf", None)
#self.is_changed = False
def __setitem__(self, key, value):
self.is_changed = True
UserDict.__setitem__(self, key, value)
def __bytes__(self): def __bytes__(self):
#if self.orig_ref is not None:
# if self.is_changed:
# if self.pdf is not None:
# del self.pdf.xref_table[self.orig_ref.object_id]
# else:
# return bytes(self.orig_ref)
out = bytearray(b"<<") out = bytearray(b"<<")
for key, value in self.items(): for key, value in self.items():
if value is None: if value is None:
@ -248,17 +234,12 @@ class PdfDict(UserDict):
out.extend(bytes(PdfName(key))) out.extend(bytes(PdfName(key)))
out.extend(b" ") out.extend(b" ")
out.extend(value) out.extend(value)
#out += b"\n%s %s" % (PdfName(key), value)
out.extend(b"\n>>") out.extend(b"\n>>")
return bytes(out) return bytes(out)
#return out + b"\n>>" #return out + b"\n>>"
__str__ = __bytes__ if str == bytes:
__str__ = __bytes__
#def write(self, f, orig_ref=None, pdf=None):
# self.orig_ref = orig_ref
# self.pdf = pdf
# f.write(bytes(self))
class PdfBinary: class PdfBinary:
@ -290,7 +271,7 @@ def pdf_repr(x):
elif isinstance(x, list): elif isinstance(x, list):
return bytes(PdfArray(x)) return bytes(PdfArray(x))
elif isinstance(x, str) and str != bytes: elif isinstance(x, str) and str != bytes:
return pdf_repr(x.encode("utf-8")) return pdf_repr(encode_text(x))
elif isinstance(x, bytes): elif isinstance(x, bytes):
return b"(" + x.replace(b"\\", b"\\\\").replace(b"(", b"\\(").replace(b")", b"\\)") + b")" # XXX escape more chars? handle binary garbage return b"(" + x.replace(b"\\", b"\\\\").replace(b"(", b"\\(").replace(b")", b"\\)") + b")" # XXX escape more chars? handle binary garbage
else: else: