The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to automate the conversion of documents to PDF format. This tool allows for easy integration with Python applications, enabling the conversion of Excel spreadsheets and other file types to PDF with a simple API call. In this tutorial, we will demonstrate how to send an API call to Convert to PDF with Python, showcasing the ease with which document conversion can be implemented in your projects.
Imagine a scenario where a business needs to convert batches of invoices, originally in Excel format, into PDFs for archival purposes. Using the Convert to PDF API, the business can automate this process, ensuring that all documents are standardized and easily accessible for future reference. This is just one example of the practical applications of the Convert to PDF API in real-world situations.
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.xlsx', 'rb'), 'application/ms-excel'), '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)
The source of the provided code is available on GitHub.
The provided Python code demonstrates how to make a multipart API call to the pdfRest Convert to PDF endpoint. Here's a breakdown of the key components:
mp_encoder_pdf = MultipartEncoder( fields={ 'file': ('file_name.tif', open('/path/to/file.xlsx', 'rb'), 'application/ms-excel'), 'output' : 'example_pdf_out', } )
This snippet creates a MultipartEncoder
object with two fields: the file to convert and the desired output filename. The 'file' field includes the file name, the file object opened in binary read mode, and the MIME type 'application/ms-excel'.
headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_pdf.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }
The headers dictionary sets the expected response format to JSON, the content type to 'multipart/form-data', and includes the user's API key for authentication.
response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)
This line sends the POST request to the pdfRest API endpoint with the encoded data and headers.
Upon receiving a successful response, the code prints the JSON response. If the request fails, it prints the error text.
In this tutorial, we have learned how to use Python to call the pdfRest Convert to PDF API to convert an Excel file to a PDF. The code demonstrates handling file uploads, setting appropriate headers, and processing the API response. This example can be adapted for various file types and use cases where automated PDF conversion is necessary.
Explore and demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more details on how to use the service.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.