Skip to main content

Using DynamicPDF Cloud API with Python

· 4 min read
James A. Brannan

DynamicPDF Cloud API's client libraries greatly simplify adding our endpoints to your applications. But you are not limited to using one of our provided client libraries. Although a Python client library is in the works, using Python to call the Dynamic-PDF Cloud API's endpoints is straightforward.

The following three examples demonstrate the ease of using Python to call the dlex-layout, pdf-info, and pdf endpoints.

Python requests module

The Requests: HTTP for Humans Python library is an easy-to-use HTTP library that is built on the urllib3 library.

The library makes sending and HTTP requests easy and handles adding query strings to URLs and form encoding your POST data. The following three examples illustrate using Python's Requests library to call DynamicPDF Cloud API from your Python applications.

Using flex-layout Endpoint

The dlex-layout endpoint takes a file on your local system containing JSON data used by a DLEX file to create a PDF report.

info

Refer to the dlex-layout documentation for more information on using this endpoint.

The following example illustrates. The example first gets the DlexPath from DynamicPDF Cloud Storage.

info

The files in this example refer to the creating-pdf-dlex-layout-endpoint example, available from the Resource Manager samples.

The example next gets the LayoutData from your local file system. The Requests library then makes a POST request and returns the PDF as binary. Finally, the output is written into a PDF file.

import requests

dataFile = "c:/temp/creating-pdf-dlex-layout.json"
outputFile = "c:/temp/dlex-layout-python.pdf"

url = "https://api.dynamicpdf.com/v1.0/dlex-layout"
apiKey = "Bearer <api-key>"

file = {'LayoutData': open(dataFile, 'rb')}

r = requests.post(url, files=file,
headers={'Authorization': apiKey},
data={'DlexPath': 'samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex'})

with open(outputFile, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)

Using pdf-info Endpoint

The pdf-info endpoint obtains a PDF from your local file storage or DynamicPDF Cloud Storage, extracts metadata about the PDF, and then returns the extracted information as a JSON document.

info

Refer to the pdf-info documentation for more details on using this endpoint.

While the previous endpoint obtained the JSON layout data as a form's file input, the PDF's bytes are the request content here.

info

The files in this example refer to the merge-pdfs-pdf-endpoint example, available from the Resource Manager samples.

The POST command streams the file's content to the endpoint and returns a text string containing the JSON.

import requests

filePath = "c:/temp/merge-pdf/DocumentA.pdf"

url = "https://api.dynamicpdf.com/v1.0/pdf-info"
apiKey = "Bearer <api-key>"

file = open(filePath, 'rb')

r = requests.post(url, data=file, headers={'Authorization':
apiKey,'Content-Type':'application/pdf'})

print(r.text)

The example then prints the output to the console.

{
"author":null,
"subject":null,
"keywords":null,
"creator":null,
"producer":"DynamicPDF Core Suite (Generator, Merger and ReportWriter) for .NET v11.09",
"title":null,
"pages":[
{"pageNumber":1,"width":612,"height":792}
{"pageNumber":2,"width":612,"height":792}
],
"formFields":null,
"customProperties":null,
"xmpMetaData":false,
"signed":false,
"tagged":false,
"formFieldCount":0
}

Using pdf Endpoint

In this final example, you use the pdf endpoint to merge three documents. The pdf endpoint allows more complex processing by including an Instructions document.

info

Refer to the pdf documentation for more information on using this endpoint.

Use the pdf endpoint to create simple one-page PDF documents (cover pages, for example), create documents/reports using DLEX files, convert images to PDFs, and merge PDFs into a combined PDF.

info

The files in this example refer to the merge-pdfs-pdf-endpoint example, available from the Resource Manager samples.

The endpoint takes a form field submission as a POST request. It first gets the PDFs, DocumentA.pdf and DocumentB.pdf, from your local system and then get's DocumentC.pdf from your cloud storage. The local files specify the form inputs as Resource. The Instructions input refers to the JSON instructions JSON document. The endpoint first uploads the two local PDFs and the instructions document. It then opens the instructions document and parses the file.

{
"author": "Alex Smith",
"inputs": [
{
"type": "pdf",
"resourceName": "DocumentA.pdf",
"startPage": 1,
"pageCount": 1
},
{
"type": "pdf",
"resourceName": "DocumentB.pdf"
},
{
"type": "pdf",
"resourceName": "samples/merge-pdfs-pdf-endpoint/DocumentC.pdf"
}
]
}

An instructions document contains one or more pdf, dlex, html, image, or page inputs that perform processing. After processing the inputs, the instructions merge the resultant PDFs into a combined PDF.

info

Refer to the pdf inputs topic in the user documentation for more information on the pdf endpoint and inputs.

import requests

filePath1 = "c:/temp/DocumentA.pdf"
filePath2 = "c:/temp/DocumentB.pdf"
instructionsPath = "c:/temp/instructions.json"
outputFile = "c:/temp/pdf-merge-python.pdf"

url = "https://api.dynamicpdf.com/v1.0/pdf"
apiKey = "Bearer <api-key>"

files = [
('Resource', ('DocumentA.pdf', open(filePath1, 'rb'))),
('Resource', ('DocumentB.pdf', open(filePath2, 'rb'))),
('Instructions', ('instructions.json',
open(instructionsPath, 'rb')))]

r = requests.post(url, files=files,
headers={'Authorization': apiKey})

with open(outputFile, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)

As the above three examples illustrate, using the DynamicPDF Cloud API with your Python applications is easy. This ease is especially apparent when using the Requests Python library. But stay tuned; the DynamicPDF Cloud API developers are busy creating a Python client library, which we will announce as soon as it's released..