Convert Files to PDF on Linux using PHP and LEADTOOLS Cloud Services


Last month we introduced LEADTOOLS Cloud Services, a brand-new product offering from LEADTOOLS, the World Leader in Imaging SDKs. We highlighted its OCR, Barcode, and Document Conversion features, and how to use the services in a Node.js application. In this post, we’ll explore a more applied scenario.

Today, we will be showing how to Convert Files to PDF on Linux using PHP and LEADTOOLS Cloud Services. This is an exciting example for LEADTOOLS and our customers for two reasons. First, it’s on Linux! We have traditionally focused our efforts over the years on Windows development platforms like .NET, C/C++, COM, ActiveX and the like. Second, it’s PHP. One of the big reasons we created LEADTOOLS Cloud Services was for the PHP community.

Creating a Searchable PDF Archive

There are many well-established reasons to store your documents as a searchable PDF or PDF/A. It is a feature-packed, efficient, cross-platform, and future-proof document format. Going paperless is still a big deal, especially for large corporations such as banks and insurance companies. Whether you are scanning large batches of paper documents or consolidating legacy archives full of disparate file formats, LEADTOOLS Cloud Services can be used in a light-weight and effective server script that will automatically create and maintain your normalized archive.

The Code

In our example scenario, we are running a Linux server that is primarily used for file storage. Using a Web API can keep this server secure and minimal, requiring practically no new libraries or software to be installed since it is relying on nothing more than PHP, cURL, and an Internet connection.

Converting a file with LEADTOOLS Cloud Services involves two steps. First, you must send the Convert request and receive back the GUID for your request.

function GeneratePostOptions($url, $inputFile)
    //Function to generate the array of CURL options to be used.
    $appId    = "YOUR APP ID HERE";
    $password = "YOUR APP PASSWORD HERE";
    $file = new CURLFile($inputFile);
    $imageData = array('image' => $file);
    $postOptions = array(
        CURLOPT_POST => 1,
        CURLOPT_URL => $url,
        CURLOPT_USERPWD => "$appId:$password",
        CURLOPT_POSTFIELDS => $imageData,
    return $postOptions;
//Generate the CURL options to be used
$conversionRequestOptions =
GeneratePostOptions($formattedConversionURL, $inputFile);
$request = curl_init();
curl_setopt_array($request, $conversionRequestOptions);
//Set the request URL
if (!$guid = curl_exec($request)) {
    echo "There was an error processing the request.
    echo $guid;
curl_close($request); //Close the request
echo "Unique ID returned by the services: $guid

Now that your file is uploaded and being processed by LEADTOOLS Cloud Services, you will need to use the Query method to poll the services. Once complete, you can download the file.

//Poll the services to determine if the request has finished processing.
while (true) { 
    $request = curl_init();
    curl_setopt_array($request, $queryRequestOptions); //Set the request URL
    if (!$results = curl_exec($request)) {
        echo "There was an error querying the services ∖n∖r";
        echo $results;
    $decodedResponse = json_decode($results);
    $fileStatus      = $decodedResponse->{'FileStatus'};
    if ($fileStatus != 100 && $fileStatus != 123) {
        //The file has finished processing
    sleep(5); //If the file hasn't finished processing, we will wait 5 seconds before checking again.
echo "File finished processing with file status: " . $fileStatus . "∖n∖r";
//The file did not process successfully.  Refer to our documentation for the full list of File Statuses and their associated meanings.
if ($fileStatus != 200) {

echo "Results: ∖n∖r";
//Decode and parse the JSON results.
$jsonArray = $decodedResponse->{'RequestData'};
foreach ($jsonArray as $serviceResults) {
    echo "Service Type: " . $serviceResults->{'ServiceType'} . "∖n∖r";
    echo "URL List: ∖n∖r";
    foreach ($serviceResults->{'urls'} as $url) {
        // Download the file
        $path = parse_url($url, PHP_URL_PATH);
        echo "Downloading " . basename($path) . " ...∖n∖r∖n∖r";
        file_put_contents("/home/gregr/Documents/output/" . basename($path), fopen($url, "r"));
        echo $url . " downloaded∖n∖r";

And that’s it! This solution is incredibly flexible and can be fired up from pretty much any application, service, scheduler, or folder monitor to convert your files and add them into the archive.

Figure 1: Terminal output showing a successful conversion.

Figure 2: Searching the text in the PDF/A file created from our sample TIFF.

Additional Uses

Obviously, this is just a simple example but can be expanded to meet the needs of many real-world applications. As mentioned above, a simple PHP script like this can be plugged in to virtually any existing application that needs to implement document file conversion.

If you don’t want to use PHP, LEADTOOLS Cloud Services can be used in many other languages like C#, JavaScript, Python, and Perl with the same level of simplicity on a broad range of environments.


LEADTOOLS Cloud Services brings the power of LEADTOOLS document imaging to virtually any programming environment. Its affordable consumption-based pricing model and Web API architecture can get your application rolling quicker than most APIs and SDKs on the market. Visit for more information.


Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team or call us at 704-332-5532.

This entry was posted in Document Imaging and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *