Create PDF in Python using REST API

This short tutorial guides on how to create PDF in Python using REST API. You will learn to generate PDF in Python using REST Interface with the help of a Cloud-based API. It will provide details to generate file in the Cloud storage and download it on local system if required.

Prerequisite

Steps to Create PDF using Python with Low Code API

  1. Initialize the PdfApi class object with a Client ID and secret to generate a PDF
  2. Call the put_create_document() method to generate a PDF file with the defined name
  3. Call the download_file() method to download the PDF file generated in the Cloud storage
  4. Display the API response containing the file path with the folder name where the file is downloaded

These steps summarize the process to create a PDF file in Python using REST API. Create an ApiClient object by passing the Client secret/ID and instantiate the PdfApi object using the configured ApiClient. Call the put_create_document() to create an empty PDF file in the Cloud and use the download_file() method to retrieve the file on the local system.

Code to Generate PDF with Python using RESTful Service

import asposepdfcloud
from asposepdfcloud import PdfApi
from asposepdfcloud.rest import ApiException
api_client = asposepdfcloud.ApiClient("Client Secret", "ID")
pdfApi = PdfApi(api_client)
name = "output.pdf"
try:
create_response = pdfApi.put_create_document(name)
if create_response.status == "OK":
download_response = pdfApi.download_file(name)
print(download_response)
print ("Empty Pdf has been created successfully")
except ApiException as ex:
print (ex)

This code has demonstrated the process to generate PDF using Python with Low Code API. The put_create_document() returns the response object that contains status, code, document, messages, etc. properties, out of which status can be used to check if the operation is successful or not. In case of success, call the download_file() method that returns the local system path where the file is downloaded.

This guide has taught us the process of creating a PDF file. To transform a PDF file to SVG, refer to the article on Convert PDF to SVG with Python REST API.

 English