The pdfRest Convert PDF Colors API Tool is a valuable resource for developers working with PDF documents that require precise color control, especially when converting from RGB to CMYK. This tutorial will guide you through the process of using Python to send an API call to the Convert PDF Colors endpoint, allowing you to effortlessly achieve this color profile conversion.
Converting RGB (Red, Green, Blue) colors to CMYK (Cyan, Magenta, Yellow, Black) is essential for preparing PDF documents for printing. CMYK is the color model used by most printers, and accurately converting RGB colors to CMYK ensures that the printed colors match the intended appearance. This is particularly important for projects like brochures, magazines, and marketing materials where color accuracy is crucial.
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 'acrobat9-cmyk'. mp_encoder_pdfWithConvertedColors = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output' : 'example_pdfWithConvertedColors_out', 'color_profile': 'acrobat9-cmyk', } ) # 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
The code begins by importing necessary modules, including MultipartEncoder
from requests_toolbelt
, which is used to handle multipart form data. The requests
library is also imported for making HTTP requests, and json
for handling JSON responses.
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. 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': 'acrobat9-cmyk', } )
Here, MultipartEncoder
is used to prepare the data to be sent in the request. The fields
dictionary includes:
'file'
: The PDF file to be uploaded, opened in binary mode.'output'
: The desired name for the output file.'color_profile'
: The target color profile, set to 'acrobat9-cmyk' in this example.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
dictionary is constructed to include the necessary headers for the API request. The 'Content-Type'
is automatically set by MultipartEncoder
, and the 'Api-Key'
is a placeholder for your actual API key.
response = requests.post(pdf_with_converted_colors_endpoint_url, data=mp_encoder_pdfWithConvertedColors, headers=headers)
This line sends the POST request to the specified endpoint with the prepared data and headers. The response is then checked to determine if the request was successful.
In this tutorial, you have learned how to use Python to send an API request to the pdfRest Convert PDF Colors endpoint. This allows you to programmatically change the color profile of PDF documents from RGB to CMYK, which can be particularly useful in digital media workflows.
To explore more functionalities, try out all of the pdfRest API Tools in the API Lab. For detailed information on each API, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit GitHub.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.