by
426 17

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.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/37267/Merge-multiple-PDFs-using-PHP-REST-API?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/**** 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 ****/
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus