Merge Multiple PDF Files into one PDF in PHP

Aspose.Pdf for Cloud logoPortable Document Format (PDF) is a widely used file format and is now an open standard for electronic document exchange maintained by the International Organization for Standardization (ISO). Merging files into one PDF is as important a feature as creating a new PDF.

Aspose.Pdf for Cloud allows you to create, read, update, merge, split or convert PDFs in any language including .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. You can use it with any language or platform that supports REST. (Almost all platforms and languages support REST and provide native REST clients to work with REST APIs).

This post covers how to merge multiple PDF files into a single PDF file in PHP. You can check the article Merge Multiple PDF Files for other languages. To merge PDFs, you need to upload the input PDFs to Aspose for Cloud or any supported third party storage and then send a PUT request to the Aspose for Cloud service. The 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 the 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.

  • storage – This parameter can be used to set storage name if you are using a third party storage.
  • folder – This parameter can be used to set the name/path of the folder where input files have been uploaded.

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

    1. Set App SID and App Key and sign the URI.
      See section 1 of the following code and Sign URI method for more details.
    2. 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.
    3. Send a PUT request to Aspose for Cloud service.
      See section 3 of the following code and processCommand method for more details.
    4. Download merged PDF file if required.
      See section 4 of the following code.

      Below 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 our PHP SDK to merge multiple PDF files, you can download this SDK from Aspose for Cloud SDK for PHP. In order to use PHP SDK, you need to perform following steps:

        1. Set the base product URI, App SID, App Key and output location.
          See section 1 of the complete code.
        2. Upload the input PDF files.
          See section 2 of the complete code.
        3. Create an object of the Document class and call the mergeDocuments method passing an array of names (including paths) of the input PDF files.
          See section 3 of the complete code.
        4. Download the 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 ****/


          To keep up with our news, you can follow us on Twitter or follow our Facebook page.

          One Response to Merge Multiple PDF Files into one PDF in PHP

          1. Pingback: Merge Multiple PDF Files into a Single PDF File...

          Leave a Reply