Document Conversion Suite

FREEMIUM
By petadata | Updated a month ago | Data
Popularity

8.3 / 10

Latency

461ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (6)

Convert documents to PDF by using Python

In this tutorial we will cover how to implement asynchronous approach of Document Conversion Suite. You only need to send source document to SubmitTIFFConversionTask or SubmitPDFConversionTask API methods to receive task identifier as response. There is also SubmitDOCXConversionTask method to convert PDF documents to editable Microsoft Word document.

After receiving task identifier you need to check task status by calling GetConversionTaskStatus method. You need to call same method after a few seconds delay as long as task status equals to “Waiting”. Once you receive “Completed” from GetConversionTaskStatus method you can call DownloadResult method to download final document file. You can use the Python implementation below.

import requests
import time


def submit_pdf_conversion_task(file_path_to_convert, rapid_api_key):
    multipart_form_data = {
        'file': (file_path_to_convert, open(file_path_to_convert, 'rb')),
        'authorName': (None, 'Sebastian'),
        'title': (None, 'Final report'),
        'keywords': (None, 'Reports, Final'),
        'name': (None, 'Quarterly Final report')
    }

    headers = {
        'X-RapidAPI-Key': rapid_api_key
    }

    response = requests.post('https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask',
                             files=multipart_form_data, headers=headers)
    if response.status_code != 200:
        raise Exception('Cannot create task')

    return response.text


def get_conversion_task_status(task_id, rapid_api_key):
    params = {
        'taskId': task_id
    }

    headers = {
        'X-RapidAPI-Key': rapid_api_key
    }

    response = requests.get('https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus',
                            headers=headers, params=params)
    if response.status_code != 200:
        raise Exception("Cannot check task status")

    return response.text


def download_result(task_id, rapid_api_key):
    params = {
        'taskId': task_id
    }

    headers = {
        'X-RapidAPI-Key': rapid_api_key
    }

    response = requests.get('https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult', headers=headers,
                            params=params)
    if response.status_code != 200:
        raise Exception('Cannot download file')

    return response.content
		
if __name__ == '__main__':
    rapid_api_key = '<YOUR RAPIDAPI KEY HERE>'
    file_path_to_convert = '<FILE PATH TO CONVERT>'
    task_id = submit_pdf_conversion_task(file_path_to_convert, rapid_api_key)
    retry_count = 0
    while retry_count < 100:
        retry_count += 1
        time.sleep(5)
        status = get_conversion_task_status(task_id, rapid_api_key)
        if status == 'Completed':
            file_bytes = download_result(task_id, rapid_api_key)
            with open('final_report.pdf', mode='wb') as binary_pdf:
                binary_pdf.write(file_bytes)
            break
        elif status == 'Waiting':
            continue
        elif status == 'Failed':
            raise Exception('Cannot convert file')
        else:
            raise Exception('Invalid status')