具有多个页面的TCPDF和FPDI

Pau*_*MrG 4 php fpdi tcpdf

这看起来是最简单的事情,但我无法让它发挥作用.

我需要将文本添加到多页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');
Run Code Online (Sandbox Code Playgroud)

指向文档的链接

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

FPDI课程 http://www.setasign.de/support/manuals/fpdi/

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

Pau*_*MrG 11

解决了我的问题......

// 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');
Run Code Online (Sandbox Code Playgroud)

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

$this->numPages = $this->setSourceFile($fullPathToFile);
Run Code Online (Sandbox Code Playgroud)

并查看最后一个代码块 - for循环添加其余页面.

不知道这是不是应该怎么做?我在一些地方读到,甚至无法实现这一点,文档中也没有提供代码.但是,这有效,希望它可以帮助某人.


小智 6

我稍微努力了一下,并尝试用最简单的方法将一些文本添加到多页文档的最后一页.这是非常简单的代码,对我有用:

require_once('fpdf/fpdf.php');
require_once('fpdf/fpdi.php');
$pdf = new FPDI();
$fullPathToPDF = '/usr/local/common/my.pdf';
$pageCount = $pdf->setSourceFile($fullPathToPDF);
for ($i = 1; $i <= $pageCount; $i++) {
    $pdf->importPage($i);
    $pdf->AddPage();
    $pdf->useTemplate($i);
}
$pdf->SetFont('Helvetica');
$pdf->SetXY(110, 225);
$pdf->Write(8, 'A complete document imported with FPDI');
$pdf->Output($fullPathToPDF);
Run Code Online (Sandbox Code Playgroud)

只需将文件的完整路径更改为您拥有多页PDF的位置即可.