Concatenate PDF in PHP
As requirement for one project we need it to concatenate PDF files to have just one file as input.
First download in your work space TCPDF and FPDI
Class to concatenate pdf, pdfConcat.php:
require_once("tcpdf/tcpdf.php"); require_once("fpdi/fpdi.php"); class concat_pdf extends FPDI { var $files = array(); function setFiles($files) { $this->files = $files; } function concat() { foreach($this->files AS $file) { $pagecount = $this->setSourceFile($file); for ($i = 1; $i <= $pagecount; $i++) { $tplidx = $this->ImportPage($i); $s = $this->getTemplatesize($tplidx); $this->AddPage(’P', array($s['w'], $s['h'])); $this->useTemplate($tplidx); } } } }
Usage:
include_once("pdfConcat.php"); $pdf =& new concat_pdf(); $pdf->setFiles(array("doc.pdf","pauta.pdf", "4bp.pdf", "5bp.pdf")); $pdf->concat(); $pdf->Output("newpdf.pdf", "I");
After this I recommend to you to keep the file already merged in the server, then you will not need to generate it another time. To do this you can use $pdf->Output(“newpdf.pdf”, “F”); instead.
Related posts:

First off, AWESOME!
Second, is there a way to check for a returned error? I’m using this on a form and I want to have the user return to the previous page to try again if the creation fails.
Thanks,
Jim
Thanks!
ufff, I’m sorry now I really can’t remember but I’m certain there’s a way :S
First of all, great script!
Second, I have a question:
When I concatenate pdf files I get one horizontal line on each page just above my header.
It is situated 2 or 3 mm from the top end of the paper.
Is it a feature or a bug, and if the first, is there a way I could hide it?
Thanks in advance and keep writing such great scripts.
No there’s nothing in my code that is doing that.
Regards and thanks!
Hi again.
Here is what I have done step by step:
Download TCPDF – Version: 4.8.031
Download FPDF_TPL-1.1.4
Download FPDI_1.3.2
Extract the archives in one dir in my localhost workspace.
Move ‘fpdf_tpl.php’ file to ‘FPDI_1.3.2′ dir in order it could be loaded properly.
Create an index.php file.
Copy the upper ‘concat_pdf’ class in it.
Copy the upper ‘Usage’ code.
Use a standart PDF file in the setFiles() method, downloaded from the web, not previously generated with TCPDF.
(By the way, If I use a file, which is generated by TCPDF, the result is the same)
Launch the script.
The output is the same as the source PDF file with one difference – on each page there is a line 2 or 3 mm below the top edge of the page. It starts from the ‘left_margin’ position and ends to the ‘right_margin’ position.
If we use TCPDF to build a page, the same result may be achieved by the following code used in the Header() method:
$this->Line($this->left_margin, 3, $this->getPageWidth() – $this->right_margin, 3);
I think you should go to the support forums of TCPDF, no?
I solved the problem by using FPDF and ignoring the ‘fpdi2tcpdf_bridge.php’ script, which is loaded by default if an already included TCPDF class is found.
Actually, the original example uses it, instead of TCPDF.
http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/
Now the annoying line above the header is gone.