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.
function GeneratePostOptions($url, $inputFile)
{
$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;
}
$conversionRequestOptions =
GeneratePostOptions($formattedConversionURL, $inputFile);
$request = curl_init();
curl_setopt_array($request, $conversionRequestOptions);
if (!$guid = curl_exec($request)) {
echo "There was an error processing the request.
\n\r";
echo $guid;
exit;
}
curl_close($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.
while (true) {
$request = curl_init();
curl_setopt_array($request, $queryRequestOptions);
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) {
break;
}
sleep(5);
}
echo "File finished processing with file status: " . $fileStatus . "\n\r";
if ($fileStatus != 200) {
exit;
}
echo "Results: \n\r";
$jsonArray = $decodedResponse->{'RequestData'};
foreach ($jsonArray as $serviceResults) {
echo "Service Type: " . $serviceResults->{'ServiceType'} . "\n\r";
echo "URL List: \n\r";
foreach ($serviceResults->{'urls'} as $url) {
$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.
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.