Merge existing PDF with dynamically generated PDF using TCPDF

血红的双手。 提交于 2019-12-22 10:59:44

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/33969828/merge-existing-pdf-with-dynamically-generated-pdf-using-tcpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!