简体   繁体   中英

Merging PDF files with PHP

I have several PDF files created dynamically using TCPDF.

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.

All PDF's that have to be merged are stored in same directory.

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.

Also, it works fine if I run it in separate code (without including TCPDF class)

Can you give me some idea how to avoid this and merge my PDF's?

Just change the order of the Output() parameters. The order was changed in the latest FPDF version but internally both orders are supported while TCPDF only supports $name followed by $dest.

FPDI will extend the TCPDF class if it is available. If it's not available it will extend FPDF .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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