T.G
T.G

Reputation: 33

Merge existing PDF with dynamically generated PDF using TCPDF

I am generating a PDF document using TCPDF. My requirement is to merge an existing PDF content at the last page of the dynamically generated PDF.

Upvotes: 3

Views: 9841

Answers (3)

AbdulkadirFaghi
AbdulkadirFaghi

Reputation: 96

I have tried the free version of FPDI but does not support PDF version 1.5 or above.

If someone else is looking for a free solution I have used TCPDI. You can find it on github.

My project does not use composer, so I used the master branch from https://github.com/pauln/tcpdi If you are using composer, you can find some fork for composer too. Just search tcpdi on github.

Once you add it to your project, the code is quite simple.

This is a snippet from my code. I used it to save a copy of the privacy policy (a static pdf) with the user name and agreement date in footer.

// Create new PDF document
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
...
// Add the pages from the source file.
$pagecount = $pdf->setSourceFile($localPrivacy);
for ($i = 1; $i <= $pagecount; $i++) {
    $tplidx = $pdf->importPage($i);
    $pdf->AddPage();
    $pdf->useTemplate($tplidx);
    // Add agreement text in document footer
    $pdf->SetXY(15,282);
    $pdf->Cell(180, 5, "Documento approvato da {$fullName} il {$date}", 0, 0, 'C');
}
// Send PDF on output
$pdf->Output(FOLDER_PATH . DIRECTORY_SEPARATOR . "{$userId}.pdf", 'F');

Upvotes: 1

JamesG
JamesG

Reputation: 4309

By far the best solution to your problem is to use FPDI.

https://github.com/Setasign/FPDI

The way it works is that FPDI extends TCPDF so you can work with an FPDI object using all of the methods that you're used to using with TCPDF, but with the additional methods that you need to import pages from existing PDF files (setSourceFile, getTemplateSize and useTemplate).

It looks a bit daunting to set up, but if you're using Composer it is actually incredibly easy. Just add setasign/fpdi and setasign/fpdi-tcpdf to your composer.json file and then use an instance of FPDI in place of your TCPDF instance. I found I didn't even have to call class_exists('TCPDF', true) as mentioned on the github page. Once I added those other entries to composer.json and ran composer dumpautoload it just worked.

Upvotes: 2

Bob van Luijt
Bob van Luijt

Reputation: 7588

This is still in development for TCPDF: http://www.tcpdf.org/doc/code/classTCPDF__IMPORT.html#a5a9effc936e8fa461c0f6717c2d10d93

If possible you can use ZEND:

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');

If you are running on Linux, you can also run a shell command.

<?php
exec('pdfjam 1.pdf 2.pdf -o 3.pdf'); // -o = output

You can install pdfjam from here: http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic/firth/software/pdfjam/pdfjam_latest.tgz

Upvotes: 1

Related Questions