16

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
LuRsT
  • 3,973
  • 9
  • 39
  • 51

6 Answers6

12

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

Sygmoral
  • 7,021
  • 2
  • 23
  • 31
  • 1
    He has **two generated** PDFs. Your suggestion is for one generated PDF and another one that is saved on the disk. – Matteo B. Apr 01 '15 at 15:37
  • 1
    This method allows for adding multiple PDF files too, just repeat the process. And you simply do not use TCPDF's methods to generate new PDF pages yourself, so only the 'imported' PDF pages/files remain. – Sygmoral Apr 01 '15 at 19:21
  • 3
    FPDI natively supports only pdf to version 1.4. If your pdf is above 1.4 you have to purchase a parser licence – Ophiuchus Nov 09 '17 at 14:27
4

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');
?>
Flexo
  • 87,323
  • 22
  • 191
  • 272
Phuc
  • 143
  • 1
  • 7
  • 6
    For others looking here, the Zend PDF library seems to be abandoned: https://github.com/zendframework/ZendPdf – James Bell Apr 06 '17 at 15:32
3

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.

opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • The problem with that is that I have to save each pdf then merge and the erase again the pdfs, is tcpdf really unable to merge documents? :( – LuRsT Oct 27 '09 at 11:06
  • Apparently, if am correct, import is in development - http://www.tcpdf.org/doc/code/classTCPDF__IMPORT.html#a5a9effc936e8fa461c0f6717c2d10d93 – webcoder Aug 18 '15 at 08:50
3

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.

BurninLeo
  • 4,240
  • 4
  • 39
  • 56
  • And merging files with FPDI is so easy! https://www.setasign.com/products/fpdi/demos/concatenate-fake/#p-304 – Tobse Jul 30 '20 at 07:27
  • 1
    Worth noting the free version of FPDI only handles PDF <= 1.4 – Luke Jun 29 '22 at 09:28
1

Check out FPDI and FPDF_TPL. This isn't a perfect solution, but you can basically use FPDF_TPL to create a template of your PDF file and the insert it into your PDF file.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
0

FPDI works fine but has some issues with pdf 1.7 then is not suitable if you don't have the control over pdf files to merge, so a final solution is pending.