Below is an end-to-end example of submitting a Merge request using Perl programming language:
# Simple script to showcasing how to use the Merge API in the LEADTOOLS Cloud Services.
# In order to run this script, the following changes will need to be added:
# 1) Place your Application ID in the $appId variable.
# 2) Place your Application Password in the $password variable
#
# The script will perform the following operations in order:
# 1) Upload 2 files to the LEADTOOLS Cloud Services using the forMerge flag.
# 2) Make a call to the Merge end-point to combine the two files.
# 3) Parse the JSON output from the LEADTOOLS Cloud Services
#
# This script makes use of the following Perl libraries:
# 1) Http-Message - http://search.cpan.org/~oalders/HTTP-Message-6.15/
# 2) libwww-perl - http://search.cpan.org/~oalders/libwww-perl-6.33/
# 3) JSON-Parse - http://search.cpan.org/~bkb/JSON-Parse-0.55/
# 4) Try-Tiny - http://search.cpan.org/~ether/Try-Tiny-0.30/
use base 'HTTP::Message';
use HTTP::Request::Common;
use LWP::UserAgent ();
use JSON::Parse ':all';
use JSON -support_by_pp;
use Try::Tiny;
require HTTP::Request;
require HTTP::Headers;
my $servicesUrl = "https://azure.leadtools.com/api/";
# The User Agent to be used when making requests
my $ua = LWP::UserAgent->new;
my $appId = 'Replace with Application ID';
my $password = 'Replace with Application Password';
my $tiffURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif';
my $firstFileId = uploadFileForMerge($tiffURL);
print("First File ID: " . $firstFileId . "\r\n");
if(!checkFileVerification($firstFileId)){
exit;
}
my $pdfFileUrl = 'https://demo.leadtools.com/images/pdf/leadtools.pdf';
my $secondFileId = uploadFileForMerge($pdfFileUrl);
print("Second File ID: " . $secondFileId . "\r\n");
if(!checkFileVerification($secondFileId)){
exit;
}
my @@data = ();
push( @@data, {
firstPage => 1,
lastPage => -1,
fileId => $firstFileId
});
push( @@data, {
'pages' => [5,1,2,4,3],
'fileId' => $secondFileId
});
# Enum corresponding to the output format for the file. For the purposes of this script, we will be converting to PDF.
my $outputFormat = 4;
my $mergeUrl = $servicesUrl . 'Conversion/Merge?format=' . $outputFormat;
my $encodedData = to_json \@@data;
my $headers =HTTP::Headers->new(
Content_Type => 'application/json'
);
$headers->authorization_basic($appId, $password);
my $request = HTTP::Request->new(POST => $mergeUrl, $headers);
$request->content($encodedData);
my $response = $ua->request($request);
if(!$response->is_success){
print STDERR $response->status_line, "\n";
exit;
}
print("Merge request successful \n\r");
my $returnedData;
while(true){
my $headers =HTTP::Headers->new(Content_Length => 0);
$headers->authorization_basic($appId, $password);
my $url = $servicesUrl . 'Query?id=' . $firstFileId;
my $request = HTTP::Request->new(POST => $url, $headers);
my $response = $ua->request($request);
$returnedData = parse_json($response->decoded_content);
if($returnedData->{'FileStatus'} != 100 && $returnedData->{'FileStatus'} != 123){
last;
}
sleep(5);
}
print("File finished processing with file status: " . $returnedData->{'FileStatus'} . "\n");
# The file did not process successfully. Refer to our documentation for the full list of File Statuses and their associated meanings.
if($returnedData->{'FileStatus'} != 200){
exit;
}
print("Results: \n");
$parsedResponse = $returnedData->{'RequestData'};
foreach my $serviceResults (@@$parsedResponse){
print "Service Type: " . $serviceResults->{'ServiceType'} . "\n";
$urls = $serviceResults->{'urls'};
print "URL List: \n";
foreach my $url (@@$urls){
print $url . "\n";
}
}
sub checkFileVerification{
my($id) =@@_;
my $queryUrl = $servicesUrl . 'Query?id=' . $id;
my $headers = HTTP::Headers->new(
Content_Length => 0
);
$headers->authorization_basic($appId, $password);
while(true){
$request = HTTP::Request->new(POST => $queryUrl, $headers);
$response = $ua->request($request);
$returnedData = parse_json($response->decoded_content);
if($returnedData->{'FileStatus'} != 100 && $returnedData->{'FileStatus'} != 123){
last;
}
sleep(5);
}
if($returnedData->{'FileStatus'} != 122){
print("The file has failed the verification process with File Status: " . $returnedData->{'FileStatus'});
return false;
}
return true;
}
sub uploadFileForMerge{
my($fileUrl) =@@_;
my $baseUploadUrl = $servicesUrl . 'UploadFile?forMerge=true&fileurl='.$fileUrl;
my $request = POST $baseUploadUrl,
Content =>[];
# If uploading a file as multi-part content:
# my $baseUploadUrl = $servicesUrl . 'UploadFile?forMerge=true';
# my $request = POST $baseUploadUrl,
# Content_Type => 'form-data',
# Content=>[
# FILE => [ "path/to/input/file" ]
# ];
$request->authorization_basic($appId, $password);
my $response = $ua->request($request);
if(!$response->is_success){
print STDERR $response->status_line, "\n";
exit;
}
my $id = $response->decoded_content;
}