Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active August 16, 2021 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/70a16ca09378b220b309caa3543ce098 to your computer and use it in GitHub Desktop.
Save aspose-cloud/70a16ca09378b220b309caa3543ce098 to your computer and use it in GitHub Desktop.
How to merge PDF files in PHP
This gist shows the code snippets on how to merge multiple PDF files into single combined PDF using PHP
/**** Section 1 ****/
$appSID = ’b1a1b925-cbd0-40c3-b7d5-075c93601243’
$appKey = ‘343ebf767f3f53537a45ced31d6be34f’;
//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 ****/
use AsposeCloudCommonAsposeApp;
use AsposeCloudCommonUtils;
use AsposeCloudCommonProduct;
use AsposeCloudStorageFolder;
use AsposeCloudPDFDocument;
/**** Section 1 ****/
// define Client credentials
AsposeApp::$appSID = “b1a1b925-cbd0-40c3-b7d5-075c93601243”;
AsposeApp::$appKey = “343ebf767f3f53537a45ced31d6be34f”;
// set the API base URI
$baseProductUri = “http://api.aspose.com/v1.1”;
AsposeApp::$outPutLocation = getcwd() . “/Output/”;
/**** End Section 1 ****/
/**** Section 2 ****/
// name of input PDF files to be merged
$inputFile1 = getcwd() . “/Input/input1.pdf”;
$inputFile2 = getcwd() . “/Input/input2.pdf”;
$inputFile3 = getcwd() . “/Input/input3.pdf”;
// the name of resultant PDF
$mergedFileName = “MergedFile.pdf”;
//upload input PDF files to cloud storage
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);
// initiate the PDF merge request
$result = $doc->mergeDocuments(array(‘input1.pdf’,‘input2.pdf’,‘input3.pdf’));
/**** End Section 3 ****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment