The pdfRest Convert to PDF/X API Tool is a powerful resource for ensuring that PDF documents meet the industry standards for high-quality printing and publishing. By using this tool, you can convert your PDF files to the PDF/X format, which is essential for color-accurate and reliable printing.
This tutorial will show you how to send an API call to Convert to PDF/X using Python. A real-world example of why a user might use Convert to PDF/X is when a graphic designer needs to submit a print-ready file to a printing company, ensuring that the document adheres to the strict printing standards and color consistency required by the industry.
from requests_toolbelt import MultipartEncoder import requests import json pdfx_endpoint_url = 'https://api.pdfrest.com/pdfx' # The /pdfx endpoint can take a single PDF file or id as input. mp_encoder_pdfx = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output_type': 'PDF/X-4', 'output' : 'example_pdfx_out' } ) # Let's set the headers that the pdfx 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_pdfx.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here } print("Sending POST request to pdfx endpoint...") response = requests.post(pdfx_endpoint_url, data=mp_encoder_pdfx, 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.
This code is sourced from the pdf-rest-api-samples repository on GitHub.
The provided code snippet showcases how to interact with the pdfRest API to convert a PDF file to the PDF/X standard using Python. Here's a breakdown of the key components:
from requests_toolbelt import MultipartEncoder import requests
This imports the necessary modules. MultipartEncoder
from requests_toolbelt
is used for encoding multipart form data.
mp_encoder_pdfx = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output_type': 'PDF/X-4', 'output' : 'example_pdfx_out' } )
This creates a MultipartEncoder
object with the fields needed for the API request. The fields include the PDF file to convert, the desired output type (PDF/X-4), and the output name.
headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_pdfx.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }
These are the headers for the API request. The 'Content-Type' is set automatically by the encoder, and 'Api-Key' should be replaced with your actual API key.
response = requests.post(pdfx_endpoint_url, data=mp_encoder_pdfx, headers=headers)
This sends a POST request to the pdfRest API endpoint with the encoded data and headers.
In this tutorial, we've learned how to make a multipart API call to the pdfRest Convert to PDF/X API using Python. This allows you to automate the process of converting PDFs to the PDF/X standard, which is crucial for professional printing and publishing workflows. You can now explore and demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for more details and options.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/pdf-rest-api-samples/tree/main/Python/Endpoint%20Examples/JSON%20Payload/pdfx.py.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.