Skip to footer content

How to Compress PDF Files Using Python

In this tutorial, you will learn how to compress PDF files using IronPDF in Python. The process begins with setting up IronPDF by applying a license key to ensure full functionality. Once the library is set up, you'll load an existing PDF document named 'compressed SL image based pdf.pdf' into a PDF document object, allowing for manipulation of its contents. The key focus is on compressing images within the PDF by using the 'compressImages' method with a quality parameter set to 60. After compressing the images, the modified PDF is saved as 'compressed.pdf' in a designated folder. Running the project will generate output files in the specified location, showcasing the effectiveness of the compression. This tutorial is ideal for developers looking to manage PDFs efficiently. Additionally, viewers are encouraged to try out the software using the trial link provided in the description. Subscribe for more tutorials and resources.

# Import the necessary library. Make sure IronPDF is installed in your Python environment.
from IronPdf import PdfDocument

def compress_pdf(input_file_path, output_file_path, image_quality=60):
    """
    Compresses images within a PDF document and saves the compressed version.

    :param input_file_path: str - Path to the input PDF file.
    :param output_file_path: str - Path to save the compressed PDF file.
    :param image_quality: int - Image quality after compression (0 to 100), default is 60.
    """
    # Load the PDF document from the specified path.
    pdf_doc = PdfDocument.from_file(input_file_path)

    # Compress images using the specified quality.
    pdf_doc.compress_images(image_quality)

    # Save the compressed PDF to the specified output path.
    pdf_doc.save(output_file_path)

# Set up your file paths
input_pdf = 'compressed SL image based pdf.pdf'
output_pdf = 'compressed.pdf'

# Compress the PDF
compress_pdf(input_pdf, output_pdf)

print(f'PDF compression completed. Compressed file saved as {output_pdf}')
# Import the necessary library. Make sure IronPDF is installed in your Python environment.
from IronPdf import PdfDocument

def compress_pdf(input_file_path, output_file_path, image_quality=60):
    """
    Compresses images within a PDF document and saves the compressed version.

    :param input_file_path: str - Path to the input PDF file.
    :param output_file_path: str - Path to save the compressed PDF file.
    :param image_quality: int - Image quality after compression (0 to 100), default is 60.
    """
    # Load the PDF document from the specified path.
    pdf_doc = PdfDocument.from_file(input_file_path)

    # Compress images using the specified quality.
    pdf_doc.compress_images(image_quality)

    # Save the compressed PDF to the specified output path.
    pdf_doc.save(output_file_path)

# Set up your file paths
input_pdf = 'compressed SL image based pdf.pdf'
output_pdf = 'compressed.pdf'

# Compress the PDF
compress_pdf(input_pdf, output_pdf)

print(f'PDF compression completed. Compressed file saved as {output_pdf}')
PYTHON

Further Reading: How to Compress PDF Files in Python

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.

Install with pip

Version: 2025.6

> pip install ironpdf

Report an Issue