Merge Pdf Files With PHP

Merge PDF files with PHP

I've done this before. I had a pdf that I generated with fpdf, and I needed to add on a variable amount of PDFs to it.

So I already had an fpdf object and page set up (
And I used fpdi to import the files (
FDPI is added by extending the PDF class:

class PDF extends FPDI


$pdffile = "Filename.pdf";
$pagecount = $pdf->setSourceFile($pdffile);
for($i=0; $i<$pagecount; $i++){
$tplidx = $pdf->importPage($i+1, '/MediaBox');
$pdf->useTemplate($tplidx, 10, 10, 200);

This basically makes each pdf into an image to put into your other pdf. It worked amazingly well for what I needed it for.

PHP: Merge PDFs without composer?

You don't need composer to use PDFMerger.

Just clone the original repository from and move PDFMerge.php and the tcpdf directory into your project.

Then you can do the following:

include 'PDFMerger.php';

$pdf = new \PDFMerger\PDFMerger;

Merge two PDFs using php

I used pdftk and resolved my problem.

How to merge multiple PDF files to one one-page PDF in PHP

You can do this with FPDF and FPDI.
A good demo to start is available here. You just have to remove the drawing of borders and offsets.

Merge pdf files in PHP and keep weblinks inside

All the mentioned libraries use FPDI under the hood, which simply does not support content outside of a pages content stream, such as links or any other annotation type.

We (author of FPDI) also offer non-free products which work on another level and which allow you keep all annotations including links and also forms when you concatenate the documents. This is possible with the SetaPDF-Merger component:

$merger = new SetaPDF_Merger();

foreach($sourcePdfs as $file) {
$merger->addFile($pdfDir . '/source/' . $file);


$document = $merger->getDocument();
$document->setWriter(new SetaPDF_Core_Writer_Http('Download.pdf'));

Merge multiple PDF files into one in PHP

yes. i have merged pdf's.
Get the fpdf and fpdi libraries and put them in to the code igniter third party folder.
then it is just a matter of reading the manual for fpdfi and merging the documents.
use this to set up the libraries


then this snippet of code should give you an idea of how it works. note i have edited sections of this for clarity. this example streams the pdf file. you could just as easily save the file elsewhere.

    $files = array(<file full paths here>);

$pdf = new FPDI_Protection();
if ($data->password)
for ($i = 0; $i < count($files); $i++ )
$pagecount = $pdf->setSourceFile($files[$i]);
for($j = 0; $j < $pagecount ; $j++)
$tplidx = $pdf->importPage(($j +1), '/MediaBox'); // template index.
$pdf->addPage('L','A4');// orientation can be P|L
$pdf->useTemplate($tplidx, 0, 0, 0, 0, TRUE);
unlink($files[$i]); // you may not want to unlink the files!

$dt = new DateTime(NULL, new DateTimeZone($data->user->timezone));
// set the metadata.
$pdf->SetCreator('website name!');
$pdf->SetTitle('PDF, created: '.$dt->format(MYHMRS_DATETIME_FRIENDLY));
$pdf->SetSubject('PDF subject !');
$pdf->SetKeywords('website name!'.", keywords! ".$data->user->user_name);
$output = $pdf->Output('', 'S');
$name = "PDF".'-'.$dt->format('ymd').'.pdf';

->set_header("Content-Disposition: filename=$name;")

as for getting pdfs from other sites, firstly make sure you are allowed to do this, then it is matter of using cURL. (copy URL). there is a codeigniter library to do this, or you can use the PHP library.

How can I merge more than one PDF in single PDF in PHP

Yes of course you can using your existing FPDF library.
Simple and awesome.

Merge two PDF files into single one using MPDF

mPDF is not the best tool to merge PDF files. You'll be better off with GhostScript:

gs -dBATCH -dSAFER -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=combined.pdf invoice1.pdf invoice2.pdf

Alternatively, generate both invoices directly to one file:

$invoice_nos = ['0' => 'ISE-00000014Y18', '1' => 'ISE-00000005Y18'];
$mpdf = new \mPDF('utf-8', 'A4', '', '', 5, 5, 36, 10, 5, 4);

foreach ($invoice_nos as $key => $invoice_no) {
$html = 'Invoice No - ' . $invoice_no;
$mpdf->WriteHTML($html, 2);

$pdf_file_name = $invoice_no . 'invoices.pdf';
$mpdf->Output($pdf_file_name, 'f');

Related Topics

Leave a reply