Compress PDF Python Overview
The Compress PDF Python library by ConvertAPI is a tool that allows for the compression and reduction of PDF file sizes by up to 90%. This Python library optimizes PDF quality by compressing text, graphics, images, subsetting fonts, and optimizing document structure. It offers various parameters for customization, including compression presets, color and grayscale image compression, and options to remove elements like bookmarks, annotations, forms, and embedded files from the PDF. The Python library also provides options for optimizing the PDF for web viewing and preserving the PDF/A standard.
Lightning-Fast Compression
Shrink your PDFs in just a few seconds without compromising quality.
Advanced Image Recompression
Force image recompression to ensure even pre‑compressed images get smaller.
Compression without losing quality
Maintain visual quality of the original file while dramatically reducing file size.
Preserve Fonts & Vectors
Compression keeps text, fonts, and vector graphics sharp.
Multiple Compression Levels
Choose from predefined compression presets or set up your own settings.
Secure by Design
File compression is encrypted and compliant with ISO, GDPR, and HIPAA.
Customizable Parameters
Fine-tune your automation with these powerful conversion options
File
File Supported formats: .pdfFile to be converted. Value can be URL or file content.
Password
StringSets the password to open protected PDF.
Preset
Collection Default: noneChoose a compression level from the available presets. If a preset is selected, all other compression options will be ignored. These presets are optimized for various use cases, such as balancing file size and quality.
Values: none lossless text archive web ebook printerImageResolution
Integer Default: 150Set the maximum image resolution in DPI.
Range: 10 .. 800ImageQuality
Integer Default: 80Set image quality in percentage.
Range: 10 .. 100UnembedBaseFonts
Bool Default: FalseSpecifies whether to remove the base fonts from the PDF file.
SubsetEmbeddedFonts
Bool Default: FalseOutput PDF should only contain font characters utilized in the original document, and any unused glyphs from all fonts in the document should be removed.
Optimize
Bool Default: TrueOptimize page content streams in the PDF file.
Linearize
Bool Default: FalseLinearize PDF file and optimize for fast Web View.
PreservePdfa
Bool Default: FalsePreserve the PDF/A standard in the PDF file.
RemoveForms
Bool Default: FalseRemove PDF forms from the PDF file.
RemoveDuplicates
Bool Default: TrueRemove duplicate fonts and color profiles from the PDF file.
RemovePieceInformation
Bool Default: TrueRemove piece information dictionaries like Adobe Illustrator or Photoshop private data.
RemoveEmbeddedFiles
Bool Default: TrueRemove embedded/attachments files from the PDF file.
RemoveStructureInformation
Bool Default: TrueRemove all structure information from the PDF document.
RemoveMetadata
Bool Default: TrueDetach the XMP metadata embedded in the document from its catalog, clear all properties in all schemas, and remove metadata from marked content.
RemoveUnusedResources
Bool Default: TrueRemove references to resources such as fonts, images, patterns.
StoreFile
Bool Default: FalseWhen the StoreFile parameter is set to True, your converted file is written to ConvertAPI’s encrypted, temporary storage and made available via a time-limited secure download URL, valid for up to 3 hours. After this period, the file is permanently deleted.
When StoreFile is set to False, conversion happens entirely in-memory. The raw file bytes are streamed back in the API response without touching disk or external storage, ensuring maximum security and zero persistence so that only you can access the content.
Integrate using Python
Ready-to-run code samples for quick conversion and automation.
A detailed guide to PDF compression using Python
ConvertAPI provides a wide variety of converter suites. One of our popular converters is PDF Compression API. It can reduce a document's size by up to 99% while maintaining the same visual clarity. You can use the Python programming language to compress PDF documents easily. We created an SDK library for Python to avoid any explicit HTTP calls - we handle it for you. In this tutorial, we will go through the steps needed to use Python for PDF compression.
PDF Document compression algorithm
Our PDF Compression API applies multiple techniques with configurable options to reduce the size of the PDF. These include PDF structure optimization, linearizing the document, and subsetting embedded fonts, so only the used characters are included in the PDF assets. It will also allow you to remove multiple objects from the final PDF, like alternative images, unused fonts, duplicate elements, annotations, bookmarks, etc. The PDF compressor can preserve the PDF/A format and handle the compression for password-protected documents using Python. It is easier to achieve than it sounds using our ConvertAPI library for Python programming language.
How to use Python to compress a PDF document?
Compressing PDF documents using Python is super simple. Follow these steps to reduce the document's size programmatically:
- Install the ConvertAPI library
- Get your API Token
- Set up the conversion parameters
- Copy-paste the code into your project
Install ConvertAPI library for Python
The first thing you want to do is to install our Python library into your project. Here you have two options. You can install it using pip:
pip install --upgrade convertapi
Or you can use our library's source code from GitHub by using:
python setup.py installSign up for a free account
Secondly, please sign up for a free account on the ConvertAPI website to retrieve your API Token key.

Set up PDF compression parameters
Once you have your library installed and found your API Token, using the Python library for PDF compression is super simple. You can set up all conversion parameters and test the compression result using our PDF Compression API interactive demo page.
Get your auto-generated code snippet
As soon as you are happy with the conversion result, please find an auto-generated code snippet at the bottom of the page. All parameters in the code snippet are generated dynamically based on your choices, so there is no more coding involved - copy-paste the code snippet into your project, and you are good to go!
Code example
An extended example of a PDF compression in the Python programming language might look something like this:
convertapi.api_credentials = 'api_token'
convertapi.convert('compress', {
'File': '/path/to/large.pdf',
'ColorImageCompression': 'zip',
'ColorImageQuality': '70',
'ColorImageDownsample': 'true',
'ColorImageThresholdDpi': '150',
'ColorImageResampleDpi': '100',
'RemoveBookmarks': 'true',
'RemoveAnnotations': 'true',
'RemoveForms': 'true',
'RemovePageLabels': 'true',
'RemoveLayers': 'true',
'RemoveArticleThreads': 'true',
'RemoveNamedDestinations': 'true',
'RemoveEmbeddedFiles': 'false',
'RemovePieceInformation': 'false',
'UnembedBaseFonts': 'true',
'SubsetEmbeddedFonts': 'true',
'CreateObjectStreams': 'false',
'Linearize': 'true'
}, from_format = 'pdf').save_files('/path/to/dir')Advanced Python PDF compression options
You can convert local files from your disc drive as well as remote files accessible by a public URL, or even pass a file stream to gain all performance benefits using the ConvertAPI library for Python.
Compress a local PDF file
To compress a local PDF file stored on your machine, specify the path to the file and the destination folder where you want to store your result like so:
convertapi.api_credentials = 'api_token'
convertapi.convert('compress', {
'File': '/path/to/sample.pdf'
}, from_format = 'pdf').save_files('/path/to/dir')Compress a remote PDF accessible by a URL
If you want to compress a remote file hosted on a server, please ensure it is publicly accessible by a URL. The URL's response must be a PDF file with the appropriate "application/pdf" content type set in the header.
convertapi.api_credentials = 'api_token'
convertapi.convert('compress', {
'File': 'https://cdn.convertapi.com/cara/testfiles/document-large.pdf'
}, from_format = 'pdf').save_files('/path/to/dir')Compress a file stream
For large file processing, consider using file streams. It can increase performance significantly. Here is an example of how to pass a file stream to our converters:
import convertapi
import io
import tempfile
convertapi.api_credentials = 'api_token'
with io.FileIO("path\\to\\file", 'r') as file_stream:
result = convertapi.convert('pdf', { 'File': file_stream })
saved_files = result.save_files(tempfile.gettempdir())
print("The PDF saved to %s" % saved_files)
You can find more examples of advanced file conversion techniques in our GitHub examples repo.
Conclusion
ConvertAPI makes your Python PDF compression easy. Simply install our library and use the auto-generated code snippets from our PDF Compression API page. You can set up a fully customizable PDF compression level and improve performance with significantly reduced PDF sizes. Check out our ConvertAPI Python library on GitHub, and feel free to contribute if you have an idea of how to make it even better!
Integrate within minutes
Easy Compress PDF automation using our simple Python SDK
Compatible With all Python Frameworks & Tools
Businesses trust us
Highest rated File Conversion API on major B2B software listing platforms: Capterra, G2, and Trustpilot.
"ConvertAPI has been a game-changer for our document automation workflows. Their conversion accuracy and API reliability are unmatched in the industry for over 7 years."
"ConvertAPI is a reliable, cost-effective solution with a proven track record of stability. It has grown significantly in maturity, adopting enterprise-grade practices over the years."
"We've integrated ConvertAPI across our entire document processing platform. The performance is exceptional and the support team is always responsive. Highly recommended!"
Enterprise-Grade Security
We ensure that all document processing is handled securely in the cloud, adhering to industry-leading standards like ISO 27001, GDPR, and HIPAA. To enhance security even further, we can ensure that no files or data are stored on our servers and never leave your country.
Learn more about security