简体   繁体   English

使用 TCPDF 将现有 PDF 与动态生成的 PDF 合并

[英]Merge existing PDF with dynamically generated PDF using TCPDF

I am generating a PDF document using TCPDF.我正在使用 TCPDF 生成 PDF 文档。 My requirement is to merge an existing PDF content at the last page of the dynamically generated PDF.我的要求是在动态生成的 PDF 的最后一页合并现有的 PDF 内容。

By far the best solution to your problem is to use FPDI .到目前为止,您问题的最佳解决方案是使用FPDI

https://github.com/Setasign/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 ).它的工作方式是, FPDI扩展TCPDF让您可以与工作FPDI使用所有的你已经习惯了使用方法的对象TCPDF ,但与其他方法,你需要从现有的PDF文件导入页面( setSourceFilegetTemplateSizeuseTemplate )。

It looks a bit daunting to set up, but if you're using Composer it is actually incredibly easy.设置看起来有点令人生畏,但如果您使用 Composer,它实际上非常容易。 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.只需将setasign/fpdisetasign/fpdi-tcpdf到您的composer.json文件,然后使用FPDI实例代替您的TCPDF实例。 I found I didn't even have to call class_exists('TCPDF', true) as mentioned on the github page.我发现我什至不必像 github 页面上提到的那样调用class_exists('TCPDF', true) Once I added those other entries to composer.json and ran composer dumpautoload it just worked.一旦我将这些其他条目添加到composer.json并运行composer dumpautoload它就可以工作了。

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

If possible you can use ZEND:如果可能,您可以使用 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.如果您在 Linux 上运行,您还可以运行 shell 命令。

<?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您可以从这里安装 pdfjam: http ://www2.warwick.ac.uk/fac/sci/statistics/staff/academic/firth/software/pdfjam/pdfjam_latest.tgz

I have tried the free version of FPDI but does not support PDF version 1.5 or above.我尝试过 FPDI 的免费版本,但不支持 PDF 1.5 或更高版本。

If someone else is looking for a free solution I have used TCPDI.如果其他人正在寻找免费的解决方案,我已经使用了 TCPDI。 You can find it on github.你可以在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.我的项目没有使用 composer,所以我使用了https://github.com/pauln/tcpdi的 master 分支如果你使用的是 composer,你也可以找到一些 composer 的 fork。 Just search tcpdi on github.只需在 github 上搜索 tcpdi。

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.我用它来保存隐私政策的副本(静态 pdf),页脚中包含用户名和协议日期。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM