How to Create a Blank PDF with Python
Why Create Blank PDF with Python?
The pdfRest Create Blank PDF API Tool is a powerful utility that allows developers to generate blank PDF documents programmatically. This tutorial will guide you through the process of sending an API call to the Create Blank PDF endpoint using Python. By leveraging this tool, you can automate the creation of PDF documents, which can be beneficial for various applications where generating blank PDFs is necessary.
In a real-world scenario, you might use the Create Blank PDF feature to generate placeholders for documents that will be filled in later. For example, a company might need to create blank invoice templates that are later populated with customer-specific data. Automating this process with the pdfRest API can save time and reduce the potential for human error, making it an invaluable tool for businesses that handle large volumes of documents.
Create Blank PDF with Python Code Example
from requests_toolbelt import MultipartEncoder
import requests
import json
# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"
blank_pdf_endpoint_url = api_url + '/blank-pdf'
# The /blank-pdf endpoint generates an empty PDF, so no file upload is required.
mp_encoder_blank_pdf = MultipartEncoder(
fields={
'page_size': 'letter',
'page_count': '3',
'page_orientation': 'portrait',
}
)
# Let's set the headers that the blank-pdf endpoint expects.
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_blank_pdf.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
print("Sending POST request to blank-pdf endpoint...")
response = requests.post(blank_pdf_endpoint_url, data=mp_encoder_blank_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)
# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-multipart.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 working with JSON data.
api_url = "https://api.pdfrest.com"
This line sets the base URL for the API. You can switch to an EU-based service by uncommenting the alternative URL for GDPR compliance.
blank_pdf_endpoint_url = api_url + '/blank-pdf'
This constructs the full URL for the blank PDF endpoint by appending /blank-pdf to the base URL.
mp_encoder_blank_pdf = MultipartEncoder(
fields={
'page_size': 'letter',
'page_count': '3',
'page_orientation': 'portrait',
}
)
The MultipartEncoder is used to create the payload for the request. The fields specify the page size, count, and orientation of the PDF. According to the pdfRest Cloud API Reference Guide, these parameters define the properties of the generated PDF.
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_blank_pdf.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
The headers are set to specify the expected response format (application/json), the content type of the request, and the API key for authentication. The API key must be replaced with your own.
response = requests.post(blank_pdf_endpoint_url, data=mp_encoder_blank_pdf, headers=headers)
This line sends a POST request to the API endpoint with the specified data and headers. The response is then processed to check for success and print the result.
Beyond the Tutorial
In this tutorial, you learned how to use Python to make an API call to the pdfRest Create Blank PDF endpoint. This allows you to programmatically generate blank PDFs with specific properties. To explore more features and capabilities of pdfRest, you can try out all the API Tools in the API Lab.
For more detailed information on each API call and its parameters, refer to the API Reference Guide. Note that this example uses a multipart API call, and you can find code samples using JSON payloads at GitHub.