In this tutorial, we will introduce how to compress a pdf file in python. The best way to compress a pdf file is use other application to compress pdf file using python. For example: Use python call ghostscript.
How to use python to compress pdf file?
We can use pdf_compressor.py, You can download it in here.
This python script use python to call ghostscript to compress pdf file.
How to use pdf_compressor.py to compress pdf file?
You can use compress() function to compress.
power can be:
quality = { 0: '/default', 1: '/prepress', 2: '/printer', 3: '/ebook', 4: '/screen' }
where
/default
/prepress: 300dpi
/printer: 300dpi
/ebook: 150dpi
/screen: 72dpi
For example:
compress("source.pdf", 'compressed.pdf')