Saturday, 27 June 2015

Merge multiple PDFs using PHP REST API


PHP developers can learn how to merge multiple PDF files into a single PDF file using PHP REST API and SDK. To merge PDFs, you need to upload input PDFs to Aspose for Cloud Service or any supported third party storage and then send a PUT request to Aspose for Cloud service. 

This REST example uses PHP CURL library to send HTTP request and handle HTTP response so you need to install CURL to use these examples. Once input PDF files are uploaded, you can use the following URI to merge PDFs on Aspose for Cloud or any supported third party storage.


http://api.aspose.com/v1.1/pdf/MergedFile.pdf/merge 


You can use a couple of optional parameters with the above mentioned URI. All or specific parameters can be used according to your requirement.


After building URI, you need to go through the following steps:


- Set App SID and App Key and sign URI. See section 1 of the following code and Sign URI method for more details.
- Build JSON to post in the request body. A list of input documents including their paths should be provided. See section 2 of the following code.
- Send a PUT request to Aspose for Cloud service. See section 3 of the following code and processCommand method for more details.
- Download merged PDF file if required. See section 4 of the following code.

/**** Section 1 ****/       

$appSID = '77****-****-****-****-80*********';
$appKey = '****************';
//build URI to merge PDFs
$strURI = 'http://api.aspose.com/v1.1/pdf/MergedFile.pdf/merge';
//sign URI
$signedURI = sign($strURI, $appSID, $appKey);

/**** End Section 1 ****/

/**** Section 2 ****/

//Build JSON to post
$documentsList = array('List'=> array('input1.pdf', 'input2.pdf', 'input3.pdf'));
$json = json_encode($documentsList);

/**** End Section 2 ****/

/**** Section 3 ****/
$responseStream = processCommand($signedURI, 'PUT', 'json', $json);

/**** End Section 3 ****/

/**** Section 4 ****/

//Download merged PDF
//build URI
$strURI = 'http://stage.aspose.com/v1.1/storage/file/MergedFile.pdf';
//sign URI
$signedURI = sign($strURI, $appSID, $appKey);
$responseStream = processCommand($signedURI, "GET", "", "");
$outputPath = getcwd() . '/output/MergedFile.pdf';
saveFile($responseStream, $outputPath);
echo 'Files have been merged and output file has been saved at: ' . $outputPath;

/**** End Section 4 ****/

0 comments:

Post a Comment