How to Merge PDFs in PHP

Sheeraz Gul Feb 02, 2024
  1. Use Ghostscript to Merge Multiple PDF Files in PHP
  2. Use FPDF and FPDI Libraries to Merge Multiple PDFs in PHP
How to Merge PDFs in PHP

PHP has a few different ways to merge multiple PDFs.

Ghostscript was the most used way to perform PDF operations through PHP for a long time.

FPDI is also a library of PHP that offers PDF operations in PHP.

Merging multiple PDFs can be done both ways. This tutorial demonstrates how to install and run these libraries on PHP to merge PDF files.

Use Ghostscript to Merge Multiple PDF Files in PHP

The Ghostscript is a command-line library that is not built-in on windows. First, we need to install the Ghostscript from the link https://ghostscript.com/releases/gsdnld.html.

Download the Ghostscript file according to your version of the operating system. The file will be something like this:

gs9550w64.exe

Run the downloaded file and install it. Once the installation is complete, go to the installation directory’s bin folder and rename the file gswin64c.exe to gs.exe to use gs as a command.

The installation of Ghostscript is complete. The next step is to use PHP to merge pdf files with Ghostscript.

<?php
$files= array("one.pdf","two.pdf","three.pdf");
$pdfdir = "C:/Apache24/htdocs/samplepdfs/";
//we gave absolute path because sometimes the libraries can't detect the path. Please use your path here.
$output = $pdfdir."NewMergedPDF.pdf";
$cmd = "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=".$output." -dBATCH ";
//setting enviroment variable path
putenv('PATH=C:\Program Files\gs\gs9.55.0\bin');

foreach($files as $file) {
    //Setting path for each file
    $pdf= $pdfdir.$file;
	//adding each file to the command
    $cmd = $cmd.$pdf." ";
}
// The final command will be the comment below, if you run this command directly in cmd, the output will be similar
//gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=C:/Apache24/htdocs/samplepdfs/NewMergedPDF.pdf -dBATCH C:/Apache24/htdocs/samplepdfs/one.pdf C:/Apache24/htdocs/samplepdfs/two.pdf C:/Apache24/htdocs/samplepdfs/three.pdf
exec($cmd);
?>

The code above takes three PDF files as input and merges them into one PDF.

Output:

Merge Multiple PDFs Using Ghostcript

Use FPDF and FPDI Libraries to Merge Multiple PDFs in PHP

First, we need to download the FPDF and FPDI libraries from the links below.

After downloading the zip or any other files, extract them into the folder from running your test.php file.

Once done extracting the files, run the example below:

<?php
//import fpdi and fpdf files
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi/src/autoload.php');

//create your class to merge pdfs
class MergePdf extends Fpdi
{
    public $pdffiles = array();

    public function setFiles($pdffiles)
    {
        $this->pdffiles = $pdffiles;
    }
    //function to merge pdf files using fpdf and fpdi.
    public function merge()
    {
        foreach($this->pdffiles AS $file) {
            $pdfCount = $this->setSourceFile($file);
            for ($pdfNo = 1; $pdfNo <= $pdfCount; $pdfNo++) {
                $pdfId = $this->ImportPage($pdfNo);
                $temp = $this->getTemplatesize($pdfId);
                $this->AddPage($temp['orientation'], $temp);
                $this->useImportedPage($pdfId);
            }
        }
    }
}

$Outputpdf = new MergePdf();
//we gave absolute path because sometimes the libraries can't detect the path. Please use your path here.
$Outputpdf->setFiles(array('C:\Apache24\htdocs\samplepdfs\one.pdf', 'C:\Apache24\htdocs\samplepdfs\two.pdf', 'C:\Apache24\htdocs\samplepdfs\three.pdf'));
$Outputpdf->merge();

//I: The output pdf will run on the browser
//D: The output will download a merged pdf file
//F: The output will save the file to a particular path.
//We select the default I to run the output on the browser.
$Outputpdf->Output('I', 'Merged PDF.pdf');
?>

The code above will import the fpdi and fpdf libraries and merge the pdf files. The output will be a merged pdf run on the browser.

Output:

Merge Multiple PDFs Using FPDF and FPDI

Other libraries can also perform similar operations on PDF, but the Ghostscript and Fpdi are the two most widely used.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP PDF