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 PHP implementation below.

  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

function submitPdfConversionTask($sourceFilePath, $rapidApiKey) {
    $curl = curl_init();
    $fields = [
        'authorName' => 'John K.',
        'title' => 'Curriculum vitae',
        'subject' => 'My latest cv',
        'keywords' => 'CV, Personal',
        'file' => new \CurlFile($sourceFilePath, 'text/plain', $sourceFilePath)
    ];

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_FAILONERROR => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $fields,
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: petadata-document-conversion-suite.p.rapidapi.com",
            "X-RapidAPI-Key: $rapidApiKey"
        ],
    ]);

    $response = curl_exec($curl) or die(curl_error($curl));
    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close ($curl);
    if($http_status == 200) {
        return $response;
    }
}

function getConversionTaskStatus($taskId, $rapidApiKey) {
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => "https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus?taskId=$taskId",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: petadata-document-conversion-suite.p.rapidapi.com",
            "X-RapidAPI-Key: $rapidApiKey"
        ],
    ]);

    $response = curl_exec($curl) or die(curl_error($curl));
    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close ($curl);
    if($http_status == 200) {
        return $response;
    }
}

function downloadResult($fileName, $taskId, $rapidApiKey)
{
    $curl = curl_init();
    $fp = fopen($fileName, 'w+');
    curl_setopt_array($curl, [
        CURLOPT_URL => "https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult?taskId=$taskId",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_FILE => $fp,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: petadata-document-conversion-suite.p.rapidapi.com",
            "X-RapidAPI-Key: $rapidApiKey"
        ],
    ]);

    curl_exec($curl) or die(curl_error($curl));
    curl_close ($curl);
    fclose($fp);
}

$rapidApiKey = "<YOUR RAPIDAPI KEY HERE>";
$filePathToConvert = "<FILE PATH TO CONVERT>";
$taskId = submitPdfConversionTask($filePathToConvert, $rapidApiKey);
$retryCount = 0;
while ($retryCount < 100){
    $retryCount++;
    sleep(5);
    $status = getConversionTaskStatus($taskId, $rapidApiKey);
    if ($status == "Completed")
    {
        downloadResult("final_report.pdf", $taskId, $rapidApiKey);
        break;
    }
    else if ($status == "Waiting")
    {
        continue;
    }
    else if ($status == "Failed")
    {
        die("Cannot convert file");
    }
    else
    {
        die("Invalid status");
    }
}