PyPDF2 compression

nagordon picture nagordon · Apr 1, 2014 · Viewed 9.3k times · Source

I am struggling to compress my merged pdf's using the PyPDF2 module. this is my attempt based on http://www.blog.pythonlibrary.org/2012/07/11/pypdf2-the-new-fork-of-pypdf/

import PyPDF2
path = open('path/to/hello.pdf', 'rb')
path2 = open('path/to/another.pdf', 'rb')
merger = PyPDF2.PdfFileMerger()
merger.append(fileobj=path2)
merger.append(fileobj=path)
pdf.filters.compress(merger)
merger.write(open("test_out2.pdf", 'wb'))

The error I receive is

TypeError: must be string or read-only buffer, not file

I have also tried to compressing the pdf after the merging is complete. I am basing my failed compression on what file size I got after using PDFSAM with compression. Any thoughts? Thanks.

Answer

xilopaint picture xilopaint · Mar 26, 2018

PyPDF2 doesn't have a reliable compression method. That said, there's a compressContentStreams() method with the following description:

Compresses the size of this page by joining all content streams and applying a FlateDecode filter.

However, it is possible that this function will perform no action if content stream compression becomes "automatic" for some reason.

Again, this won't make any difference in most cases but you can try this code:

import PyPDF2

path = 'path/to/hello.pdf'
path2 = 'path/to/another.pdf'
pdfs = [path, path2]

writer = PyPDF2.PdfFileWriter()

for pdf in pdfs:
    reader = PyPDF2.PdfFileReader(pdf)
    for i in xrange(reader.numPages):
        page = reader.getPage(i)
        page.compressContentStreams()
        writer.addPage(page)

with open('test_out2.pdf', 'wb') as f:
    writer.write(f)