How to Convert PDF Files from Color to Black and White (Grayscale) with Python

Learn how to use Convert PDF Colors API Tool from pdfRest to convert color PDFs to black and white with Python
Share this page

Why Convert PDFs from Color to Black and White with Python?

The pdfRest Convert PDF Colors API Tool is a powerful resource for developers who need to manipulate the color profiles of PDF documents programmatically. This tutorial will guide you through the process of sending an API call to convert a color PDF to black and white using Python. By leveraging this API, you can automate the process of converting color PDFs to black and white, which is especially useful for creating print-ready documents or reducing file size.

In a real-world scenario, a document preparation specialist might need to convert a colorful PDF report into a grayscale version for archival purposes or to reduce printing costs. By using the Convert PDF Colors API, this conversion can be done quickly and efficiently, saving time and reducing the potential for human error.

Convert PDFs from Color to Black and White 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 'gamma-22'.
mp_encoder_pdfWithConvertedColors = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_pdfWithConvertedColors_out',
        'color_profile': 'gamma-22',
    }
)

# 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

The code begins by importing necessary libraries: requests_toolbelt for handling multipart form data, requests for making HTTP requests, and json for parsing JSON data.

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

This line sets the URL for the Convert PDF Colors API endpoint. This is where the POST request will be sent.

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

The MultipartEncoder is used to encode the fields for the multipart form data. The 'file' field contains the PDF file to be converted. The 'output' field specifies the name of the output file. The 'color_profile' field is set to 'gamma-22', which is the desired color profile for the conversion.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdfWithConvertedColors.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

The headers are set to specify that the response should be in JSON format. The 'Content-Type' is automatically set to 'multipart/form-data' by the MultipartEncoder. The 'Api-Key' is a placeholder for your actual API key, which authenticates your request.

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

This line sends the POST request to the API endpoint with the encoded data and headers. The response is stored in the response variable.

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

If the request is successful, the response is parsed as JSON and printed in a formatted manner. If not, the error message is printed.

Beyond the Tutorial

In this tutorial, you learned how to use the pdfRest Convert PDF Colors API with Python to change the color profile of a PDF document from color to grayscale. This can be particularly useful for black and white print optimization. We encourage you to explore all the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.

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

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