Creating a new PDF by Merging PDF documents using TCPDF
Asked Answered
T

6

17

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.

Talesman answered 27/10, 2009 at 10:49 Comment(0)
M
14

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

Moulding answered 25/9, 2014 at 11:36 Comment(3)
He has two generated PDFs. Your suggestion is for one generated PDF and another one that is saved on the disk.Mixon
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.Moulding
FPDI natively supports only pdf to version 1.4. If your pdf is above 1.4 you have to purchase a parser licenceMallard
E
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');
?>
Election answered 18/4, 2011 at 9:13 Comment(1)
For others looking here, the Zend PDF library seems to be abandoned: github.com/zendframework/ZendPdfProvincial
W
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.

Wolsey answered 27/10, 2009 at 11:3 Comment(2)
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? :(Talesman
Apparently, if am correct, import is in development - tcpdf.org/doc/code/…Volatile
L
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.

Liquidate answered 28/6, 2020 at 15:10 Comment(2)
And merging files with FPDI is so easy! setasign.com/products/fpdi/demos/concatenate-fake/#p-304Aleece
Worth noting the free version of FPDI only handles PDF <= 1.4Woolgathering
P
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.

Periodicity answered 31/3, 2010 at 5:48 Comment(0)
A
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.

Arnica answered 10/4, 2023 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.