Compress PDF Files in Python — A Simple Guide

Andrew Wilson
2 min readJun 4, 2024

--

Compressing PDF files is a great way to save storage space on your computer and makes sharing and transferring documents easier. By implementing the task with Python, you can automate the process of reducing PDF file sizes and managing large numbers of documents.

In this article, we will explore a simple way to compress PDF files in Python.

Python Library for Reducing PDF File Size

First, we need to install the Spire.PDF for Python library, which allows to reduce the size of PDF files by adjusting the image quality and compressing the embedded fonts.

The Python PDF library can be installed using the following pip command:

pip install Spire.PDF

How to Compress PDF Files with Python

Steps:

1. Import the library.

2. Create an instance of the PdfCompressor class, and pass in a sample PDF that needs to be compressed.

3. Get the optimization options through the PdfCompressor.OptimizationOptions property.

4. Compress fonts by setting the value of the OptimizationOptions.SetIsCompressFonts() method to True.

5. Set the image quality (High/Mediem/Low) using the OptimizationOptions.SetImageQuality() method.

6. Resize images by setting the value of the OptimizationOptions.SetResizeImages() method to True.

7. Compress images by setting the value of the OptimizationOptions.SetIsCompressImage() method to True.

8. Compress the PDF file and save it using PdfCompressor.CompressToFile() method.

Python Code:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfCompressor object
compressor = PdfCompressor("report.pdf")

# Get the optimization options
options = compressor.OptimizationOptions

# Compress fonts
options.SetIsCompressFonts(True)

# Set image quality
options.SetImageQuality(ImageQuality.Medium)

# Resize image
options.SetResizeImages(True)

# Compress image
options.SetIsCompressImage(True)

# Compress the PDF file and save it
compressor.CompressToFile("CompressPDF.pdf")

Input & Output:

Python compress PDF

The Spire.PDF for Python library gives us the flexibility to tailor the compression options to specific needs. For maximum compression, we can set the image quality to Low. In addition, we can set to unembed fonts via the SetIsUnembedFonts() method.

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.