How to Compress PDFs Programmatically in Python (5 Lines of Code)
If you're building a web app that handles document uploads, you know the pain: massive PDF files slow down your S3 uploads, bloat your storage costs, and frustrate your users.
While libraries like PyPDF2 are great for merging files, they often struggle with actual file size reduction. To get professional-grade compression (up to 80% reduction) without sacrificing quality, using a high-performance REST API is the modern standard.
Here is how to do it using Python and the PDF Squeezer API.
The 5-Line Implementation
You'll need the requests library. If you don't have it, run pip install requests.
import requests
# 1. Your API Key (Get yours at pdfsqueezer.io)
headers = {'Authorization': 'Bearer YOUR_API_KEY'}
url = 'https://api.pdfsqueezer.io/v1/compress'
# 2. Open the file and send the request
with open('large_document.pdf', 'rb') as f:
response = requests.post(url, files={'file': f}, headers=headers)
# 3. Save the "squeezed" version
if response.status_code == 200:
with open('optimized_document.pdf', 'wb') as f:
f.write(response.content)
print("Compression Successful!")
Why use an API instead of a local library?
- Sub-Second Latency: Our globally distributed endpoints handle the heavy lifting so your server CPU stays cool.
- Object-Level Optimization: We don't just "zip" the file; we intelligently downsample images and scrub hidden metadata.
- No Dependencies: Avoid the headache of installing complex C-libraries (like Ghostscript) on your production server.
Advanced Configuration
Want more control? You can pass parameters to fine-tune the balance between quality and size:
params = {
'quality': 75, # 1-100 scale
'stripMetadata': True, # Removes EXIF and private data
'maxDimension': 1600 # Resizes oversized images
}
response = requests.post(url, files={'file': f}, headers=headers, params=params)
Integrating with AWS S3 or Google Cloud
For most production apps, you'll want to compress the file before it hits your long-term storage. By inserting PDF Squeezer into your upload pipeline, you can save thousands of dollars in egress and storage fees as your app scales.
Frequently asked questions
How do I compress a PDF in Python?
Use the requests library to POST your PDF to the PDF Squeezer API at https://api.pdfsqueezer.io/v1/compress. Send the file as multipart/form-data with an Authorization: Bearer YOUR_API_KEY header. The API returns the compressed PDF in the response body.
Why use a PDF compression API instead of PyPDF2?
PyPDF2 excels at merging and basic PDF manipulation but often struggles with real file size reduction. A REST API like PDF Squeezer performs object-level optimization—downsampling images, stripping metadata, and compressing streams—delivering up to 80% file size reduction without heavy server-side dependencies.
What parameters control PDF compression quality?
Pass quality (1–100), stripMetadata (removes EXIF and private data), and maxDimension (resizes oversized images, e.g. 1600px). Higher quality values produce larger files; lower values yield more compression.
Can I compress PDFs before uploading to S3?
Yes. Call the PDF Squeezer API in your upload pipeline before the file reaches long-term storage. This reduces S3 storage costs and egress fees as your application scales.
Ready to optimize your document workflow?
Stop wasting bandwidth on unoptimized files. Sign up for a free API key at pdfsqueezer.io and start squeezing today.
Get free API key