How to Compress PDF with Python

Learn how to make a PDF smaller with Python by calling the Compress PDF API Tool from pdfRest.
Share this page

Why Compress PDFs via API?

The pdfRest Compress PDF API Tool is a powerful resource for developers and businesses looking to reduce the size of their PDF files without compromising on quality. This tutorial will guide you through the process of making an API call to the Compress PDF endpoint using Python. Compressing PDFs is particularly useful in scenarios where you need to save storage space, speed up file transfers, or meet file size requirements for email attachments or website uploads.

Python Code Example for Compression

from requests_toolbelt import MultipartEncoder
import requests
import json

compressed_pdf_endpoint_url = 'https://api.pdfrest.com/compressed-pdf'

# The /compressed-pdf endpoint can take a single PDF file or id as input.
# This sample demonstrates setting compression_level to 'medium'.
# We have preset 'high', 'medium', and 'low' compression levels available for use. These preset levels do not require the 'profile' parameter.
mp_encoder_compressedPdf = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_compressedPdf_out',
        'compression_level': 'medium',
    }
)

# Let's set the headers that the compressed-pdf endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_compressedPdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to compressed-pdf endpoint...")
response = requests.post(compressed_pdf_endpoint_url, data=mp_encoder_compressedPdf, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

The code above is sourced from the pdfRest API samples repository on GitHub, specifically from this file.

Explaining the Python Code

The code snippet provided is a Python script that uses the pdfRest Compress PDF API to compress a PDF file. Let's break down the code:

from requests_toolbelt import MultipartEncoder
import requests
import json

This section imports the necessary modules. MultipartEncoder from requests_toolbelt is used for encoding multipart form data. The requests module is used to make HTTP requests, and json is used for JSON parsing.

mp_encoder_compressedPdf = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_compressedPdf_out',
        'compression_level': 'medium',
    }
)

This creates a MultipartEncoder object with fields for the API request. The fields include:

  • 'file': A tuple containing the file name, a file object opened in binary read mode, and the MIME type.
  • 'output': The desired name for the output file.
  • 'compression_level': The level of compression to apply ('high', 'medium', or 'low').
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_compressedPdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

Headers for the API request are set here. The 'Content-Type' is automatically set by the MultipartEncoder. You must replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

response = requests.post(compressed_pdf_endpoint_url, data=mp_encoder_compressedPdf, headers=headers)

This line sends the POST request to the Compress PDF API endpoint with the data and headers.

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

After receiving the response, the script checks if the request was successful (response.ok). If so, it prints the JSON response. Otherwise, it prints the error text.

Compression and So Much More

In this tutorial, we walked through a Python script that calls the pdfRest Compress PDF API to compress a PDF file. By following the steps outlined, you can integrate this functionality into your own applications. To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference documentation at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at this GitHub repository.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us