简体   繁体   English

用 PHP 合并 PDF 文件

[英]Merging PDF files with PHP

I have several PDF files created dynamically using TCPDF.我有几个使用 TCPPDF 动态创建的 PDF 文件。

I have to merge those PDF's created by TCPDF into one, and as I saw best practice is to do that with FPDI library.我必须将这些由 TCPPDF 创建的 PDF 合并为一个,正如我所看到的,最佳实践是使用 FPDI 库来做到这一点。

All PDF's that have to be merged are stored in same directory.必须合并的所有 PDF 都存储在同一目录中。

To merge them, I'm using next code:为了合并它们,我使用下一个代码:

require( MY_APP_PATH . 'fpdf/fpdf.php');
require( MY_APP_PATH . 'fpdi/fpdi.php');


$fpdi = new FPDI();
// iterate over array of files and merge
foreach ($filesToMerge as $file) {
    $fpdi->setSourceFile(MY_APP_PATH . 'pdf/' . $file);
    $tpl = $fpdi->importPage(1, '/MediaBox');
    $fpdi->addPage();
    $fpdi->useTemplate($tpl);
}

$fpdi->Output('F', 'merged.pdf');

Error I'm getting here is:我在这里的错误是:

TCPDF ERROR: Incorrect output destination: /VAR/WWW/HTML/MYAPP/PDF/MERGED.PDF

Looks like there is some collision between TCPDF and FPDI libraries (or even FPDF?), since they both have have same method Output.看起来 TCPDF 和 FPDI 库(甚至 FPDF?)之间存在一些冲突,因为它们都有相同的方法输出。

Also, it works fine if I run it in separate code (without including TCPDF class)此外,如果我在单独的代码中运行它(不包括 TCPDF 类),它工作正常

Can you give me some idea how to avoid this and merge my PDF's?你能给我一些想法如何避免这种情况并合并我的 PDF 吗?

Just change the order of the Output() parameters.只需更改Output()参数的顺序即可。 The order was changed in the latest FPDF version but internally both orders are supported while TCPDF only supports $name followed by $dest.该顺序在最新的 FPDF 版本中已更改,但内部支持这两个顺序,而 TCPDF 仅支持 $name 后跟 $dest。

FPDI will extend the TCPDF class if it is available.如果可用,FPDI 将扩展TCPPDF 类。 If it's not available it will extend FPDF .如果它不可用,它将扩展FPDF

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

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