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.qualityis 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 toTrue, 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
| Quality | Use Case |
|---|---|
| 90–100 | Near-lossless, minimal size reduction |
| 60–80 | Good balance for most documents |
| 30–50 | Small files for email or web distribution |
| 1–29 | Maximum compression, significant quality loss |






