How to Convert PDF Colors with Python

Learn how to use pdfRest Convert PDF Colors API tool with Python to change PDF color profiles
Share this page

Why Convert PDF Colors with Python?

The pdfRest Convert PDF Colors API Tool is a powerful utility that allows users to convert the color profile of a PDF document. This tutorial will guide you through the process of sending an API call to convert PDF colors using Python. By following this guide, you will learn how to utilize the pdfRest API to change the color profile of your PDF files programmatically.

Imagine you are a graphic designer who needs to ensure that all PDF documents adhere to a specific color profile, such as sRGB, for consistent printing results. Using the Convert PDF Colors API, you can automate this process, saving time and reducing the risk of human error. This tool is especially useful for large batches of documents, ensuring uniformity across all your files.

Convert PDF Colors with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

pdf_with_converted_colors_endpoint_url = 'https://api.pdfrest.com/pdf-with-converted-colors'

# The /pdf-with-converted-colors endpoint can take a single PDF file or id as input.
# This sample demonstrates setting color_profile to 'srgb'.
mp_encoder_pdfWithConvertedColors = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_pdfWithConvertedColors_out',
        'color_profile': 'srgb',
    }
)

# Let's set the headers that the pdf-with-converted-colors 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_pdfWithConvertedColors.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to pdf-with-converted-colors endpoint...")
response = requests.post(pdf_with_converted_colors_endpoint_url, data=mp_encoder_pdfWithConvertedColors, 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.

Source: GitHub

Breaking Down the Code

Let's break down the provided code to understand how it works:

pdf_with_converted_colors_endpoint_url = 'https://api.pdfrest.com/pdf-with-converted-colors'

This line sets the endpoint URL for the Convert PDF Colors API.

mp_encoder_pdfWithConvertedColors = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_pdfWithConvertedColors_out',
        'color_profile': 'srgb',
    }
)

Here, we create a MultipartEncoder object to handle the multipart form data. The fields dictionary includes:

  • file: The PDF file to be converted. This is specified with the file name, file object, and MIME type.
  • output: The desired output file name.
  • color_profile: The color profile to convert to, in this case, 'srgb'.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdfWithConvertedColors.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

We set the headers for the request. The Content-Type is automatically set to 'multipart/form-data' by the MultipartEncoder. The Api-Key should be replaced with your actual API key.

response = requests.post(pdf_with_converted_colors_endpoint_url, data=mp_encoder_pdfWithConvertedColors, headers=headers)

This line sends a POST request to the API endpoint with the multipart data and headers.

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

We check if the response is successful. If so, we print the JSON response. Otherwise, we print the error message.

Beyond the Tutorial

In this tutorial, you learned how to use the pdfRest Convert PDF Colors API tool with Python to change the color profile of a PDF document. This example demonstrates a multipart API call, but you can also explore JSON payload options in our GitHub repo.

To explore more API tools, visit the API Lab. For detailed documentation, refer to the API Reference Guide.

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