开发者

Creating a new PDF by Merging PDF documents using 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开发者_JAVA技巧 want to use other libs.


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


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜