pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

Merging multiple PDFs appears to duplicate resources per page

Open mikeokner opened this issue 10 years ago • 3 comments

I'm using PyPDF2 to merge thousands of individual PDFs (generated by WeasyPrint) into a single PDF. The resulting PDF looks fine with all of the pages merged, but the resulting file is quite large. I believe this is due to fonts/images being unnecessarily duplicated per-page.

>>> from PyPDF2 import PdfFileReader
>>> pdf = PdfFileReader(f)
>>> p0 = pdf.getPage(0)                                                                                                                                                                                                                                                         
>>> p1 = pdf.getPage(1)
>>> p0['/Resources']                                                                                                                                                                                                                                                            
{'/Font': {'/f-0-0': IndirectObject(19, 0), '/f-1-0': IndirectObject(15, 0), '/f-2-0': IndirectObject(23, 0)}, '/XObject': {'/x7': IndirectObject(28, 0), '/x5': IndirectObject(27, 0)}, '/ExtGState': {'/a0': {'/ca': 1, '/CA': 1}}}
>>> p1['/Resources']                                                                                                                                                                                                                                                            
{'/Font': {'/f-0-0': IndirectObject(41, 0), '/f-1-0': IndirectObject(37, 0), '/f-2-0': IndirectObject(45, 0)}, '/XObject': {'/x7': IndirectObject(50, 0), '/x5': IndirectObject(49, 0)}, '/ExtGState': {'/a0': {'/ca': 1, '/CA': 1}}}

For /Font and at least one of the /XObject images, I believe the IndirectObjects should all point to the same object ID as they are identical in the initial separate PDFs.

mikeokner avatar Dec 17 '15 22:12 mikeokner

In a similar situation with lots of duplicated images, a workaround that worked for me was to post-process the pdf with ghostscript using -dDetectDuplicateImages=true as suggested on http://stackoverflow.com/questions/10450120/optimize-pdf-files-with-ghostscript-or-other#answer-10453202

jerome-nexedi avatar Jul 22 '16 03:07 jerome-nexedi

I think https://github.com/py-pdf/PyPDF2/pull/207 might have changed the situation. Can you check again?

MartinThoma avatar Jun 30 '22 05:06 MartinThoma

Without having the PDF that has duplications, I cannot check myself.

MartinThoma avatar Jun 30 '22 05:06 MartinThoma