Document Conversion Suite

フリーミアム
よって petadata | 更新済み एक महीने पहले | Data
人気

8 / 10

レイテンシー

426ms

サービスレベル

100%

Health Check

N/A

すべてのチュートリアルに戻る (6)

Convert documents to PDF by using PHP

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.

<?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', basename($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");
    }
}