diff --git a/pdfrw/pdfwriter.py b/pdfrw/pdfwriter.py index 3c887ba..cb884cb 100755 --- a/pdfrw/pdfwriter.py +++ b/pdfrw/pdfwriter.py @@ -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) @@ -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 = {} @@ -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 diff --git a/tests/test_examples.py b/tests/test_examples.py index 6871b80..aac2d00 100755 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -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: @@ -109,7 +109,7 @@ def do_test(self, params, prev_results=[''], scrub=False): if expects: if len(expects) == 1: expects, = expects - self.assertEqual(hash, expects) + self.assertEqual(hash, expects, '{} file does not have the expected MD5 hash'.format(dstf)) else: self.assertIn(hash, expects) result = 'pass' diff --git a/tests/test_roundtrip.py b/tests/test_roundtrip.py index 2e097b6..62bdcc9 100755 --- a/tests/test_roundtrip.py +++ b/tests/test_roundtrip.py @@ -91,7 +91,7 @@ def roundtrip(self, testname, basename, srcf, decompress=False, if expects: if len(expects) == 1: expects, = expects - self.assertEqual(hash, expects) + self.assertEqual(hash, expects, '{} file does not have the expected MD5 hash'.format(dstf)) else: self.assertIn(hash, expects) result = 'pass'