Compress PDFs with the Python API
Use the PDF Squeezer PDF compression REST API from Python in a few lines. Send a file and optional query parameters to get an optimized PDF back.
import requests
import os
API_KEY = os.environ.get('PDF_SQUEEZER_KEY')
API_URL = 'https://api.pdfsqueezer.io/v1/compress'
params = {
'quality': 85, # 1-100: image compression quality
'maxDimension': 1600, # max width/height for image resizing
'stripMetadata': True, # remove EXIF and other metadata
'convertToJpeg': True # convert images to JPEG for smaller size
}
with open('original.pdf', 'rb') as f:
files = {'file': f}
headers = {'Authorization': f'Bearer {API_KEY}'}
response = requests.post(API_URL, files=files, headers=headers, params=params)
if response.status_code == 200:
with open('optimized.pdf', 'wb') as f:
f.write(response.content)
print('Compression successful.')
print(f"Ratio: {response.headers.get('X-Compression-Ratio')}")
else:
print('API Error:', response.text)