php - 具有多个页面的 TCPDF 和 FPDI

标签 php tcpdf fpdi

这看起来是最简单的事情,但我无法让它工作。

我需要将文本添加到多页 pdf 的第一页(可以是任意页数)

在两页 pdf 上使用此代码(没有 for 循环,仅使用 $pdf->importPage(2))我最终得到两页,但第二页是第一页的重复。文本只写在第一页上,这很好,但我需要输出 pdf 中包含的所有页面。这是我的代码

// Original file with multiple pages 
$fullPathToFile = 'full/path/to/file.pdf';

class PDF extends FPDI {

    var $_tplIdx;

    function Header() {

        global $fullPathToFile;

        if (is_null($this->_tplIdx)) {

            $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);

        }
        $this->useTemplate($this->_tplIdx);

    }

    function Footer() {}

}

// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);


// add a page
$pdf->AddPage();

// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');

// How to get the number of pages of original pdf???
// $numPages = $pdf->getNumPages(???);

// Carry on adding all remaining pages starting from page 2
for($i=2;$i<=$numPages;$i++) {
    // Add another page
    $pdf->AddPage();
    // Do I need to declare the source file here?
    // $pdf->setSourceFile($fullPathToWD);
    $pdf->importPage($i);
}

// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');

文档链接

TCPDF 类 http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced

FPDI 类(class) http://www.setasign.de/support/manuals/fpdi/

FPDF_TPL 类 http://www.setasign.de/support/manuals/fpdf-tpl/

最佳答案

解决了我的问题...

// Original file with multiple pages 
$fullPathToFile = 'full/path/to/file.pdf';

class PDF extends FPDI {

    var $_tplIdx;

    function Header() {

        global $fullPathToFile;

        if (is_null($this->_tplIdx)) {

            // THIS IS WHERE YOU GET THE NUMBER OF PAGES
            $this->numPages = $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);

        }
        $this->useTemplate($this->_tplIdx);

    }

    function Footer() {}

}

// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);


// add a page
$pdf->AddPage();

// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');

// THIS PUTS THE REMAINDER OF THE PAGES IN
if($pdf->numPages>1) {
    for($i=2;$i<=$pdf->numPages;$i++) {
        $pdf->endPage();
        $pdf->_tplIdx = $pdf->importPage($i);
        $pdf->AddPage();
    }
}

// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');

您可以通过添加这一行的第一部分来获得页数

$this->numPages = $this->setSourceFile($fullPathToFile);

然后查看倒数第二个代码块 - for 循环添加页面的其余部分。

不知道是否应该这样做?我在一些地方读到它甚至不可能实现这一点,而且文档中也没有提供代码。但是,这有效,希望它对某人有所帮助。

关于php - 具有多个页面的 TCPDF 和 FPDI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14546275/

相关文章:

php - 使用 LIMIT 和 BETWEEN 运算符的类似子查询的较短 SQL 语句

php - Javascript (jQuery) - $.post 的问题

php - 布局中的 FPDF 图像

javascript - 如何从自动完成文本框中单击名称?

php - Osclass 在侧边栏上的显示位置

php - Tcpdf多重签名

wordpress - 我正在使用 TCPDF 在我的 wordpress 插件中生成 pdf,但在执行时它在已发送的库 header 上有错误。

PHP MySQL : Saving PDF to Database

php - 尝试通过 Composer 包含 setasign/fpdf

php - 无法使用 FPDI 编辑现有的 PDF 文件