En este tutorial veremos cómo implementar el enfoque asíncrono de Document Conversion Suite. Solo tienes que enviar el documento de origen a los métodos SubmitTIFFConversionTask o SubmitPDFConversionTask de la API para recibir un identificador de tarea como respuesta. También existe el método SubmitDOCXConversionTask para convertir documentos PDF en documentos editables de Microsoft Word.

Después de recibir el identificador de tarea, consulta su estado mediante el método GetConversionTaskStatus. Vuelve a llamar al mismo método tras esperar unos segundos mientras el estado sea “Waiting”. Cuando GetConversionTaskStatus devuelva “Completed”, llama a DownloadResult para descargar el documento final. Puedes utilizar la siguiente implementación en Python.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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')