php TCPDF 为一个文档中的不同页面设置不同的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9513145/
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 set different headers for different pages in one document
提问by bububaba
Is there a way to have a different header logo for the 1st page in a document and different for the 2nd page?
有没有办法为文档中的第一页使用不同的页眉徽标,而为第二页使用不同的页眉徽标?
I thought that changing the header data between adding pages might do the trick, but in my tests it seems that setting the header after adding the first page has no effect:
我认为在添加页面之间更改标题数据可能会奏效,但在我的测试中,似乎在添加第一页后设置标题无效:
/* other stuff
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->AliasNbPages();
*/
$pdf->SetHeaderData("logo_1.png", PDF_HEADER_LOGO_WIDTH, '', '');
$pdf->AddPage();
$pdf->writeHTML($htmlContent, true, 0, true, true);
$pdf->SetHeaderData("logo_2.png", PDF_HEADER_LOGO_WIDTH, '', '');
$pdf->AddPage();
$pdf->writeHTML($htmlContent2, true, 0, true, true);
The above produces a document with 2 pages, both having logo_1.png
in header.
上面产生了一个有 2 页的文档,都logo_1.png
在页眉中。
Will I need to customize TCPDF itself? Has anyone done this? I'm using version 5.9.144
.
我需要自定义 TCPPDF 本身吗?有没有人做过这个?我正在使用版本5.9.144
。
采纳答案by LR1234567
Strange. I'm having the same issue, but this worked in my older version of TCPDF version:4.8.009 and I noticed the issue when I upgraded to 5.9.149.
奇怪的。我遇到了同样的问题,但这在我旧版本的 TCPDF 版本:4.8.009 中有效,当我升级到 5.9.149 时我注意到了这个问题。
I compared the 2 and isolated the issue to the Header() function.
我比较了 2 并将问题与 Header() 函数隔离。
I could force it to allow me to change the header and accept it by running this: $pdf->setHeaderTemplateAutoreset(true);
我可以强制它允许我更改标题并通过运行以下命令接受它: $pdf->setHeaderTemplateAutoreset(true);
回答by Joaquin
The following worked for me,
以下对我有用,
class MYPDF extends TCPDF{
function header1(){
//print whatever the header 1 is
}
function Header2(){
if($this->page==1){
//print header 1 and whatever the header 2 is
}else{
//print just header 2
}
}
}
回答by user3417572
I used:
我用了:
$pdf->resetHeaderTemplate();
It should override the template header and assign the new one according to need. It worked for me.
它应该覆盖模板标头并根据需要分配新的标头。它对我有用。
回答by starlocke
How about... have TCPDF generate pages with different headers as separate documents, and then use something to merge all those intermediate PDFs together to form the final document's pages (maybe even TCPDF itself can merge, I don't know)?
怎么样...让TCPDF生成具有不同标题的页面作为单独的文档,然后使用某些东西将所有这些中间PDF合并在一起以形成最终文档的页面(甚至TCPDF本身也可以合并,我不知道)?
A couple of "how to merge?" results:
一对夫妇“如何合并?” 结果:
回答by bububaba
Just for the record, if someone has the same problem in the future and can use Zend_Pdf
:
仅供记录,如果有人将来遇到同样的问题并且可以使用Zend_Pdf
:
// $filename is the final filename with path to save the generated PDF
$dir = dirname($filename);
$base = basename($filename);
$page1 = $dir . DIRECTORY_SEPARATOR . "tmp_1_" . $base;
$page2 = $dir . DIRECTORY_SEPARATOR . "tmp_2_" . $base;
//creates 1st page with TCPDF and saves to filesystem with filename $page1
$this->generateInvoicePage1($html_1, $page1);
//creates 2nd page with TCPDF and saves to filesystem with filename $page2
$this->generateInvoicePage2($html_2, $page2);
$pdf1 = Zend_Pdf::load($page1);
$pdf2 = Zend_Pdf::load($page2);
foreach ($pdf2->pages as $page) {
$pdf1->pages[] = clone($page);
}
$pdf1->save($filename);
unlink($page1);
unlink($page2);
回答by WebOnLife
If you wish to have a cover page without header and footer and internal pages with them, there is an easier way to handle it. Simply turn off the header and footer print via 'setPrintHeader' and 'setPrintFooter' as follows:
如果你希望有一个没有页眉和页脚的封面和带有它们的内部页面,有一个更简单的方法来处理它。只需通过“setPrintHeader”和“setPrintFooter”关闭页眉和页脚打印,如下所示:
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$pdf->SetFont("freesans", "B", 20);
$pdf->Cell(0,10,"COVER TEXT",1,1,'C');
$pdf->setPrintHeader(true);
$pdf->setPrintFooter(true);
$pdf->setHeaderFont(array("freesans", "", 9));
$pdf->SetHeaderData('', '', 'Document Title', 'Document Header Text');
$pdf->AddPage();
$pdf->SetFont("freesans", "B", 20);
$pdf->Cell(0,10,"Internal text",1,1,'C');
$pdf->Output("HappyCover.pdf", "I");
Enjoy!
享受!
回答by Jim Nolan
I found this to be the solution with the lightest touch:
我发现这是最轻巧的解决方案:
class MYPDF extends TCPDF {
//Page header
public function AddNewHeader($newTitle) {
$this->header_xobj_autoreset = true;
$this->header_title = $newTitle;
}
}
Be sure to call TCPDF::setHeaderData() first. Next, call this function before every AddPage() event, or, if you're looping through data and relying on tcpdf to add pages, call it after every element add. It breaks the caching of the header, but allows the user to put a new and custom header on each page. All the elements returned by TCPDF::getHeaderData() can be updated in this way.
一定要先调用 TCPDF::setHeaderData()。接下来,在每个 AddPage() 事件之前调用此函数,或者,如果您循环访问数据并依赖 tcpdf 添加页面,则在每个元素添加之后调用它。它破坏了标题的缓存,但允许用户在每个页面上放置一个新的自定义标题。TCPPDF::getHeaderData() 返回的所有元素都可以通过这种方式更新。