Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a .Pages attribute in PdfWriter to allow setting its MediaBox or Resources fields #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pdfrw/pdfwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def format_obj(obj):
if compress and obj.stream:
do_compress([obj])
pairs = sorted((getattr(x, 'encoded', None) or x, y)
for (x, y) in obj.iteritems())
for (x, y) in iteritems(obj))
myarray = []
for key, value in pairs:
myarray.append(key)
Expand Down Expand Up @@ -262,6 +262,7 @@ def __init__(self, fname=None, version='1.3', compress=False, **kwargs):
"on PdfWriter instance" % name)
setattr(self, name, value)

self.Pages = None
self.pagearray = PdfArray()
self.killobj = {}

Expand Down Expand Up @@ -310,14 +311,14 @@ def _get_trailer(self):
self.make_canonical()

# Create the basic object structure of the PDF file
pages = self.Pages or IndirectPdfDict()
pages.Type = PdfName.Pages
pages.Count = PdfObject(len(self.pagearray))
pages.Kids = self.pagearray
trailer = PdfDict(
Root=IndirectPdfDict(
Type=PdfName.Catalog,
Pages=IndirectPdfDict(
Type=PdfName.Pages,
Count=PdfObject(len(self.pagearray)),
Kids=self.pagearray
)
Pages=pages,
)
)
# Make all the pages point back to the page dictionary and
Expand Down
5 changes: 3 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def do_test(self, params, prev_results=[''], scrub=False):
os.remove(dstf)
if scrub and os.path.exists(scrub):
os.remove(scrub)
subprocess.call(params)
subprocess.check_output(params)
if scrub:
PdfWriter(dstf).addpages(PdfReader(scrub).pages).write()
with open(dstf, 'rb') as f:
Expand All @@ -109,7 +109,8 @@ def do_test(self, params, prev_results=[''], scrub=False):
if expects:
if len(expects) == 1:
expects, = expects
self.assertEqual(hash, expects)
assert_error_msg = '{} file does not have the expected MD5 hash'.format(dstf)
self.assertEqual(hash, expects, assert_error_msg)
else:
self.assertIn(hash, expects)
result = 'pass'
Expand Down
3 changes: 2 additions & 1 deletion tests/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def roundtrip(self, testname, basename, srcf, decompress=False,
if expects:
if len(expects) == 1:
expects, = expects
self.assertEqual(hash, expects)
assert_error_msg = '{} file does not have the expected MD5 hash'.format(dstf)
self.assertEqual(hash, expects, assert_error_msg)
else:
self.assertIn(hash, expects)
result = 'pass'
Expand Down