PDF Compression

IronPDF for Python can reduce the file size of PDF documents by compressing embedded images using the CompressImages method, which accepts a quality level and an optional image-scaling parameter.

Getting Started

Load a PdfDocument from a file using PdfDocument(path), call CompressImages with the desired quality level, and save the result to a new file.

Understanding the Code

  • PdfDocument("document.pdf"): Opens the existing PDF for modification.
  • CompressImages(quality): Compresses all images in the document to the specified quality level. quality is an integer from 1 to 100, where 100 preserves the original quality and lower values reduce file size at the cost of image fidelity.
  • CompressImages(quality, scaleDownImages): The optional second boolean parameter, when set to True, additionally scales down image resolution based on each image's visible size in the PDF. This can produce significantly smaller files but may cause visual artifacts for some image types.
  • SaveAs(path): Saves the compressed PDF to the specified file path.

Choosing a Quality Level

QualityUse Case
90–100Near-lossless, minimal size reduction
60–80Good balance for most documents
30–50Small files for email or web distribution
1–29Maximum compression, significant quality loss

Learn to optimize PDFs with IronPDF for Python!

Ready to Get Started?
Version: 2026.6 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.