PyPDF2压缩 [英] PyPDF2 compression
问题描述
我正在努力使用PyPDF2模块压缩合并的pdf.这是我基于 http: //www.blog.pythonlibrary.org/2012/07/11/pypdf2-the-new-fork-of-pypdf/
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'))
我收到的错误是
TypeError: must be string or read-only buffer, not file
在合并完成后,我还尝试压缩pdf.我将失败的压缩基于使用PDFSAM进行压缩后得到的文件大小. 有什么想法吗?谢谢.
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.
推荐答案
PyPDF2没有可靠的压缩方法.也就是说,有一个compressContentStreams()
方法,其内容如下:
PyPDF2 doesn't have a reliable compression method. That said, there's a compressContentStreams()
method with the following description:
通过加入所有内容流并应用FlateDecode过滤器来压缩此页面的大小.
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)
这篇关于PyPDF2压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!