Jumaila Shahzad

Jumaila Shahzad

  • NA
  • 0
  • 9.8k

PHP in Cloud โ€“ Code Sample for Merging Multiple PDF files in

Mar 13 2014 3:17 AM

I am using Aspose.PDF for Cloud API to run my code, you can use any cloud services you like.

Following steps describe the process in detail.

Merge multiple PDFs using PHP REST

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 this URI to merge PDFs on Aspose for Cloud or any supported third party storage.


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.

 

Following is the code to merge multiple PDF files.

 

/**** 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 ****/

 

Merge multiple PDFs using PHP SDK

If you want to use Aspose PHP SDK to execute mail merge, you can download this SDK from Aspose for Cloud SDK for PHP. In order to use PHP SDK, you need to perform following steps.

 

-          Set base product URI, App SID, App Key and output location. See section 1 of the complete code.

-          Upload input PDF files. See section 2 of the complete code.

-          Create object of Document class and call mergeDocuments method passing array of names (including paths) of input PDF files. See section 3 of the complete code.

-          Download merged PDF file. See Download a Particular File example for more details.

Following is the complete code.

use Aspose\Cloud\Common\AsposeApp;

use Aspose\Cloud\Common\Utils;

use Aspose\Cloud\Common\Product;

use Aspose\Cloud\Storage\Folder;

use Aspose\Cloud\Pdf\Document;

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

AsposeApp::$appSID = "77****-****-****-****-80*********";

AsposeApp::$appKey = "******************";

$baseProductUri= "http://api.aspose.com/v1.1";

AsposeApp::$outPutLocation = getcwd() . "/Output/";

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

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

$inputFile1 = getcwd() . "/Input/input1.pdf";

$inputFile2 = getcwd() . "/Input/input2.pdf";

$inputFile3 = getcwd() . "/Input/input3.pdf";

$mergedFileName = "MergedFile.pdf";

//upload input PDF files

echo "Uploading main document...<br />";

$folder = new Folder();

$folder->UploadFile($inputFile1, "");

$folder->UploadFile($inputFile2, "");

$folder->UploadFile($inputFile3, "");

echo "input files uploaded <br />";

 

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

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

echo "Merging PDF files... <br />";

//create Document object

$doc = new Document($mergedFileName);

$result = $doc->mergeDocuments(array('input1.pdf','input2.pdf','input3.pdf'));

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

 


Answers (1)