Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / PHP
Article

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

13 Aug 2018CPOL 6.2K   3  
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.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

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 article, we’ll explore a more applied scenario.

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.

PHP
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_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_USERPWD => "$appId:$password",
        CURLOPT_FORBID_REUSE => 1,
        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.
\n\r";
    echo $guid;
    exit;
}
curl_close($request); //Close the request
 
echo "Unique ID returned by the services: $guid
\n\r";

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.

PHP
//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;
        curl_close($request);
        exit;
    }
    curl_close($request);
    $decodedResponse = json_decode($results);
    $fileStatus      = $decodedResponse->{'FileStatus'};
 
    if ($fileStatus != 100 && $fileStatus != 123) {
        //The file has finished processing
        break;
    }
    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) {
    exit;
}

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.

Image 1

Figure 1: Terminal output showing a successful conversion.

Image 2

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.

Conclusion

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 https://services.leadtools.com for more information.

Download the Full LEADTOOLS Cloud Services Example

You can download the fully functional demo which includes the features discussed above. Extract the .zip file and follow the instructions in the source code comments.

Support

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 CloudSales@leadtools.com or call us at 704-332-5532.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Help desk / Support LEAD Technologies, Inc.
United States United States
Since 1990, LEAD has established itself as the world's leading provider of software development toolkits for document, medical, multimedia, raster and vector imaging. LEAD's flagship product, LEADTOOLS, holds the top position in every major country throughout the world and boasts a healthy, diverse customer base and strong list of corporate partners including some of the largest and most influential organizations from around the globe. For more information, contact sales@leadtools.com or support@leadtools.com.
This is a Organisation (No members)


Comments and Discussions

 
-- There are no messages in this forum --