具有多个页面的TCPDF和FPDI

时间:2013-01-27 10:01:04

标签: 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类 http://www.setasign.de/support/manuals/fpdi/

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

4 个答案:

答案 0 :(得分: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');

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

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

然后看第二个代码块 - for循环添加剩余的页面。

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

答案 1 :(得分: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);

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

答案 2 :(得分:1)

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
...
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(true, 10);
foreach($array as $item)
{
  $pdf->AddPage(); //add new page for new item
  $txt = some_long_long_text;
  $pdf->Write(0, $txt, '', 0, 'C', true);
  $pdf->endPage(); //do end of page
  $pdf->lastPage(); //set cursor at last page, because autopagebreak not do it
}

例如,阵列中有10个学生,您需要为每个学生创建简历。在考试中,一份简历有3页。所以在外面你得到30页的pdf,文本正确。 SetAutoPageBreak(true,10),不将光标设置在最后一页,因此您需要使用函数$pdf->lastPage();

手动完成

答案 3 :(得分:0)

该代码不起作用,试试这个:

$pdf = new PDI();
$pdf->AddPage();
$pdf->setSourceFile('zzz.pdf');
$pdf->numPages = $pdf->setSourceFile('zzz.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 20, 200);
    if($pdf->numPages>1) {
for($i=2;$i<=$pdf->numPages;$i++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($i);
    $pdf->useTemplate($tplIdx, 10, 20, 200);
}
}