The pdfRest Convert to PDF API Tool is a versatile service that allows users to convert various file types into PDF format. This tutorial will demonstrate how to make an API call to the Convert to PDF endpoint using Python. This functionality is particularly useful in scenarios where documents need to be standardized for distribution, archiving, or printing, such as converting invoices, reports, or other business documents into a universally accessible PDF format.
from requests_toolbelt import MultipartEncoder import requests import json pdf_endpoint_url = 'https://api.pdfrest.com/pdf' mp_encoder_pdf = MultipartEncoder( fields={ 'file': ('file_name.tif', open('/path/to/file', 'rb'), 'image/tiff'), 'output' : 'example_pdf_out', } ) headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_pdf.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' } print("Sending POST request to pdf endpoint...") response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, 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)
Reference: pdfRest API Python Sample Code
The code starts by importing the necessary modules: MultipartEncoder
from requests_toolbelt
, requests
, and json
.
The pdf_endpoint_url
variable holds the endpoint URL to which the API call will be made.
The MultipartEncoder
is used to encode the files and fields into a multipart/form-data format. The fields include:
'file'
: A tuple containing the file name, the file object opened in binary read mode, and the MIME type of the file.'output'
: The desired name for the output PDF file.The headers
dictionary sets the necessary HTTP headers for the request. The 'Accept' header indicates that the response should be JSON, and the 'Content-Type' is set to the content type of the multipart data. The 'Api-Key' is where you would place your own API key provided by pdfRest.
A POST request is then sent using the requests.post
method with the endpoint URL, the encoded data, and the headers. The response status code is printed out, and if the request was successful, the JSON response is printed in a formatted manner. Otherwise, the response text, which usually contains error information, is printed.
In this tutorial, we have successfully made an API call to the pdfRest Convert to PDF endpoint using Python. The code sends a file to be converted into PDF and handles the response. This example demonstrates how to work with the pdfRest API to automate document conversion processes.
To explore all of the pdfRest API Tools, you can demo them in the API Lab at https://pdfrest.com/apilab/. For further details and documentation, visit https://pdfrest.com/documentation/.
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/pdf.py.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.