使用TCPDF创建新PDF并将现有PDF导入其中

时间:2016-11-29 05:26:24

标签: tcpdf

如何使用pdf

将现有pdf文件导入新创建的TCPDF?

因此假设我正在进行发票部分。用户点击打印发票后,我将开始收集数据,然后使用PDF创建TCPDF。但是,我需要"附加"另一个现有的PDF如果可用的话。因此,假设从TCPDF生成的1个发票文件包含5个页面。然后我必须"附加"另一个现有PDF到此文件中。总共将有6页或更多,取决于现有的PDF文件。用户上传现有PDF文件。因此,现有的PDF文件将首先上传,然后将添加到新生成的发票文件中。

有没有办法实现它?

3 个答案:

答案 0 :(得分:5)

您可以通过添加FPDI

来实现这一目标
<?php
require_once('tcpdf.php');
require_once('fpdi.php');

$pdf = new FPDI();

//Merging of the existing PDF pages to the final PDF
$pageCount = $pdf->setSourceFile('existing_pdf.pdf');
for ($i = 1; $i <= $pageCount; $i++) {
    $tplIdx = $pdf->importPage($i, '/MediaBox');
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx);
}

//Your code relative to the invoice here

$pdf->Output();

答案 1 :(得分:0)

我正在解决类似问题,最后我使用PHP中的GhostScript合并PDF:

exec('gs -o ' . $merged_path
    . ' -sDEVICE=pdfwrite -dDPFSETTINGS=/prepress '
    . $first_pdf_path . ' '
    . $attachment_path);

它已经好几年了。

答案 2 :(得分:0)

我使用了TCPDI(TCPDF的扩展名)。您可以在github https://github.com/pauln/tcpdi上找到它。还有供作曲家使用的叉子。

易于使用... TCPDI扩展了TCPDF,因此构造器参数相同

$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

然后在页面之后添加现有pdf文件中的页面

$pagecount = $pdf->setSourceFile($existingPdfPath);
for ($i = 1; $i <= $pagecount; $i++) {
    $tplidx = $pdf->importPage($i);
    $pdf->AddPage();
    $pdf->useTemplate($tplidx);
}
相关问题