Access the BuildVu Microservice using Python
Table of contents
- Introduction
- Prerequisites
- Code Example
- Return result to a callback url
- Configuration Options
- Upload by URL
- Using Authentication
- Further details
Introduction
The following tutorial shows you how to convert PDF files to HTML or SVG using a hosted BuildVu cloud API, such as:
- the IDRsolutions trial and cloud subscription service
- your own self-hosted BuildVu microservice
Whilst the above services can be accessed with plain old HTTP requests, this tutorial uses our open source Python IDRCloudClient which provides a simple Python wrapper around the REST API.
Prerequisites
Using pip, install the IDRCloudClient package with the following command:
pip install IDRCloudClient
Code Example
Here is a basic code example to convert PDF files to HTML or SVG. Configuration options and advanced features can be found below.
from IDRSolutions import IDRCloudClient
client = IDRCloudClient('https://cloud.idrsolutions.com/cloud/' + IDRCloudClient.BUILDVU)
try:
result = client.convert(
# token='Token', # Required only when connecting to the IDRsolutions trial and cloud subscription service
input=IDRCloudClient.UPLOAD,
file='/path/to/exampleFile.pdf'
)
outputURL = result['downloadUrl']
client.downloadResult(result, 'path/to/output/dir')
if outputURL is not None:
print("Download URL: " + outputURL)
except Exception as error:
print(error)
Return result to a callback url
The BuildVu Microservice accepts a callback url to send the status of a conversion on completion. Using a callback url removes the need to poll the service to determine when the conversion is complete.
The callback url can be provided to the convert method as shown below.
result = client.convert(
# token='Token', # Required only when connecting to the IDRsolutions trial and cloud subscription service
input=IDRCloudClient.UPLOAD,
callbackUrl='http://listener.url',
file='/path/to/exampleFile.pdf'
)
Configuration Options
The BuildVu API accepts a stringified JSON object containing key value pair configuration options to customise your conversion. The settings should be provided to the convert method. A full list of the configuration options to convert PDF files to HTML or SVG can be found here.
settings='{"key":"value","key":"value"}'
Upload by URL
As well as uploading a local file you can also provide a URL which the BuildVu Microservice will download and then perform the conversion. To do this you should replace the input and file values in the convert method with the following.
input=IDRCloudClient.DOWNLOAD
url='http://exampleURL/exampleFile.pdf'
Using Authentication
If you have deployed your own BuildVu Microservice that requires a username and password to convert PDF files to HTML or SVG, you will need to provide them with each conversion. These are provided by passing a variable named auth to the convert method as shown below.
auth=('username', 'password'))
Further details
IDRCloudClient on GitHub
IDRCloudClient on Pypi
BuildVu Microservice API
BuildVu Microservice Use