[go: up one dir, main page]

DEV Community

IDRSolutions
IDRSolutions

Posted on

How to convert PDF to SVG in Python (Tutorial)

Introduction

You can easily convert PDF files to SVG with Python using the BuildVu microservice. With this tutorial, you will be able to use the BuildVu cloud API, which could be either:

Although these services can be accessed using standard HTTP requests, this tutorial uses our open-source Python IDRCloudClient, which offers a straightforward Python wrapper for the REST API.

Why SVGs are better for Python

SVGs can be directly rendered in web frameworks like Flask or Django, allowing seamless integration with frontend technologies and interactive dashboards.

Since SVGs are text-based XML, they are simple to manipulate programmatically with Python scripts, while PDFs require more complex parsing and offer less flexibility for dynamic or interactive web content.

Prerequisites

You can install the IDRCloudClient package via pip by running the following command:

pip install IDRCloudClient
Enter fullscreen mode Exit fullscreen mode

Code Example

Below is a simple code example for converting PDF files to SVG. Details on configuration options and advanced features are provided afterwards.

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)

Enter fullscreen mode Exit fullscreen mode

Return result to a callback URL

The BuildVu Microservice allows you to specify a callback URL to receive the conversion status after processing is finished. This approach eliminates the need to keep polling the service to check for completion. Simply include the callback URL in the parameters when calling the convert method, as shown in the following example.

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’
)

Enter fullscreen mode Exit fullscreen mode

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"}’
Enter fullscreen mode Exit fullscreen mode

Upload by URL

In addition to uploading a local file, you can specify a URL for the BuildVu Microservice to fetch and convert. To do this, update the input and file parameters in the convert method as shown below.

input=IDRCloudClient.DOWNLOAD
url=’http://exampleURL/exampleFile.pdf’
Enter fullscreen mode Exit fullscreen mode

Using Authentication

auth=(‘username’, ‘password’))

Enter fullscreen mode Exit fullscreen mode

Top comments (0)