Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new PDF by Merging PDF documents using TCPDF

Tags:

php

pdf

tcpdf

How can I create a new document using other PDFs that I'm generating?

I have methods to create some documents, and I want to merge them all in a big PDF, how can I do that with TCPDF?

I do not want to use other libs.

like image 732
LuRsT Avatar asked Oct 27 '09 10:10

LuRsT


4 Answers

TCPDF has a tcpdf_import class, added in 2011, but it is still "under development". If you don't want to use anything outside of TCPDF, you're out of luck!

But FPDI is an excellent addition to TCPDF: it's like an addon. It's as simple as this:

require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php'); // the addon

// FPDI extends the TCPDF class, so you keep all TCPDF functionality
$pdf = new FPDI(); 

$pdf->setSourceFile("document.pdf"); // must be pdf version 1.4 or below
// FPDI's importPage returns an object that you can insert with TCPDF's useTemplate
$pdf->useTemplate($pdf->importPage(1)); 

Done!

See also this question: TCPDF and FPDI with multiple pages

like image 127
Sygmoral Avatar answered Nov 12 '22 16:11

Sygmoral


Hi i think TCPDF is not able to merge pdf files.

You can try it with an shell command and

PDFTK Toolkit

So you dont have to use an other pdf library.

like image 3
opHASnoNAME Avatar answered Nov 12 '22 16:11

opHASnoNAME


Why don't you use Zend_PDF, it 's really a very good way to merge file.

<?php
require_once 'Zend/Pdf.php';

$pdf1 = Zend_Pdf::load("1.pdf");
$pdf2 = Zend_Pdf::load("2.pdf");

foreach ($pdf2->pages as $page){
$pdf1->pages[] = $page;
}

$pdf1->save('3.pdf');
?>
like image 3
Phuc Avatar answered Nov 12 '22 16:11

Phuc


This thread is from 2009, but using existing PDFs in PHP is still an issue in 2020.

After Zend_PDF has been abandoned and TCPDI does not support PHP 7, FPDI currently seems one of the few working solutions left in 2020. It can be used with TCPDF and FPDF, so existing code keeps working. And it currently seems well maintained.

like image 3
BurninLeo Avatar answered Nov 12 '22 17:11

BurninLeo