php 多页的 TCPDF 和 FPDI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14546275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TCPDF and FPDI with multiple pages
提问by PaulMrG
This looks like the simplest thing but I can't get it to work.
这看起来是最简单的事情,但我无法让它工作。
I need to add text to the first page of a multi-page pdf (could be any number of pages)
我需要将文本添加到多页 pdf 的第一页(可以是任意页数)
Using this code on a two page pdf (without the for loop, just using $pdf->importPage(2)) I end up with two pages but the second page is a repeat of page one. The text is written on the first page only which is good but I need all pages included in the output pdf. Here is my code
在两页 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');
Links to docs
文档链接
TCPDF Classes http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced
TCPDF 类 http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced
FPDI Classes http://www.setasign.de/support/manuals/fpdi/
FPDI 类 http://www.setasign.de/support/manuals/fpdi/
FPDF_TPL Classes http://www.setasign.de/support/manuals/fpdf-tpl/
回答by PaulMrG
Solved my problem...
解决了我的问题...
// 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');
You get the number of pages by adding the first part of this line
您通过添加此行的第一部分来获得页数
$this->numPages = $this->setSourceFile($fullPathToFile);
And see the second last block of code - the for loop adds the remainder of the pages.
并查看倒数第二个代码块 - for 循环添加页面的其余部分。
Don't know if this is how it should be done? I read in a few places that it wasn't even possible to achieve this, also the code is not supplied in the docs. However, this works, hope it helps someone.
不知道是不是应该这样操作?我在几个地方读到它甚至不可能实现这一点,而且文档中也没有提供代码。但是,这有效,希望它可以帮助某人。
回答by Michael Parmley
I struggled with this a little and tried to come up with simplest way to add some text to the last page of a multi-page document. Here is the very simple code that worked for me:
我对此有点挣扎,并试图想出最简单的方法将一些文本添加到多页文档的最后一页。这是对我有用的非常简单的代码:
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);
Just change the full path to file to a location where you have a multi-page PDF.
只需将文件的完整路径更改为您拥有多页 PDF 的位置即可。
回答by Yelnar
$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
}
In example, you have 10 pupils in array, and you need create resume for each. In exam, one resume have 3 pages. So in out u get pdf with 30 pages, with correct text.
SetAutoPageBreak(true, 10), not set cursor at last page, so you need to do it manually with function $pdf->lastPage();
例如,您有 10 个学生,您需要为每个学生创建简历。在考试中,一份简历有3页。因此,您将获得 30 页的 pdf,并带有正确的文本。SetAutoPageBreak(true, 10), 没有在最后一页设置光标,所以你需要用函数手动完成$pdf->lastPage();
回答by rimba
that code wont work, try this:
该代码不起作用,试试这个:
$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);
}
}

