php FPDF:决定​​何时设置页眉/页脚数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9454431/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:51:29  来源:igfitidea点击:

FPDF: Decide when to set header/footer data

phpfpdf

提问by GuZzie

I've been struggling with the header and footer data for quite some time now and thought it was time to ask it here on the forum.

一段时间以来,我一直在努力处理页眉和页脚数据,我认为是时候在论坛上提问了。

What I'm trying to do is decide that if a page is added if the header / footer should be added or not. so code-wise I want to set the header/footer to on or off when adding a page.

我要做的是决定是否添加页面,是否应添加页眉/页脚。所以在代码方面我想在添加页面时将页眉/页脚设置为打开或关闭。

I've tried to manipulate the function AddPage by setting an extra argument $setFooterHeader which default is set to true. And then trying to set this argument to false whenever I do an addPage('','',false); but it ignores it for some reason and I can't figure out why.

我试图通过设置一个默认设置为 true 的额外参数 $setFooterHeader 来操作 AddPage 函数。然后每当我执行 addPage('','',false); 时尝试将此参数设置为 false;但它出于某种原因忽略了它,我不知道为什么。

If I set the default value of the argument to false in the function itself it works like a charm, but when I try to do it in my script and set it as an argument, it totally ignores it.

如果我在函数本身中将参数的默认值设置为 false,它就像一个魅力,但是当我尝试在我的脚本中执行它并将其设置为参数时,它完全忽略了它。

Here's a code snippet of the fpdf.php file (function addPage)

这是 fpdf.php 文件的代码片段(函数 addPage)

function AddPage($orientation='', $size='', $setHeaderFooter=true) 
{ 
     // Start a new page 
     if($this->state==0) 
     $this->Open(); 
     $family = $this->FontFamily; 
     $style = $this->FontStyle.($this->underline ? 'U' : ''); 
     $fontsize = $this->FontSizePt; 
     $lw = $this->LineWidth; 
     $dc = $this->DrawColor; 
     $fc = $this->FillColor; 
     $tc = $this->TextColor; 
     $cf = $this->ColorFlag; 
     if($this->page>0) 
     { 
         // Page footer 
         if ($setHeaderFooter == true) 
         { 
             $this->InFooter = true; 
             $this->Footer(); 
             $this->InFooter = false; 
             // Close page 
             $this->_endpage(); 
         } 
      } 
     // Start new page 
     $this->_beginpage($orientation,$size,$setHeaderFooter); 
     // Set line cap style to square 
     $this->_out('2 J'); 
     // Set line width 
     $this->LineWidth = $lw; 
     $this->_out(sprintf('%.2F w',$lw*$this->k)); 
     // Set font 
     if($family) 
     $this->SetFont($family,$style,$fontsize); 
     // Set colors 
     $this->DrawColor = $dc; 
     if($dc!='0 G') 
     $this->_out($dc); 
     $this->FillColor = $fc; 
     if($fc!='0 g') 
     $this->_out($fc); 
     $this->TextColor = $tc; 
     $this->ColorFlag = $cf; 
     // Page header 
     if ($setHeaderFooter == true) 
     { 
         $this->InHeader = true; 
         $this->Header(); 
         $this->InHeader = false; 
     } 
     // Restore line width 
     if($this->LineWidth!=$lw) 
     { 
         $this->LineWidth = $lw; 
         $this->_out(sprintf('%.2F w',$lw*$this->k)); 
     } 
     // Restore font 
     if($family) 
     $this->SetFont($family,$style,$fontsize); 
     // Restore colors 
     if($this->DrawColor!=$dc) 
     { 
         $this->DrawColor = $dc; 
         $this->_out($dc); 
     } 
     if($this->FillColor!=$fc) 
     { 
         $this->FillColor = $fc; 
         $this->_out($fc); 
     } 
     $this->TextColor = $tc; 
     $this->ColorFlag = $cf; 
} 

Below is a code snippet of my PHP script which uses FPDF

下面是我使用 FPDF 的 PHP 脚本的代码片段

/** PHP FPDF */ 
require_once 'classes/FPDF/fpdf.php'; 
require_once 'classes/FPDI/fpdi.php'; 

class PDF extends FPDI 
{ 
     function Header() 
     { 
         $this->SetFont( 'Arial', 'B', 18 ); //set font to Arial, Bold, and 16 Size 

         //create heading with params 
         //0 - 100% width 
         //9 height 
         //"Page Heading" - With this text 
         //1 - border around it, and center aligned 
         //1 - Move pionter to new line after writing this heading 
         //'C' - center aligned 
         $this->Cell( 0, 9, 'Page Heading', 1, 1, 'C' ); 

         $this->ln( 5 ); 
     } 

     function Footer() 
     { 
         //move pionter at the bottom of the page 
         $this->SetY( -15 ); 

         //set font to Arial, Bold, size 10 
         $this->SetFont( 'Arial', 'B', 10 ); 

         //set font color to blue 
         $this->SetTextColor( 52, 98, 185 ); 

         $this->Cell( 0, 10, 'Footer Text', 0, 0, 'L' ); 

         //set font color to gray 
         $this->SetTextColor( 150, 150, 150 ); 

         //write Page No 
         $this->Cell( 0, 10, 'Page No: ' . $this->PageNo(), 0, 0, 'R' ); 
     } 
 } 

// Create new PDF object 
$pdf = new PDF('P','mm','A4'); 
$pdf->addPage('','',false); 

// Output pdf file 
$pdf->Output('test.pdf','D'); 

Your help is greatly appreciated!!

非常感谢您的帮助!!

回答by GuZzie

I have solved this issue by setting a flag outside the class and use this flag in the header and footer function

我已经通过在类外设置一个标志并在页眉和页脚函数中使用这个标志来解决这个问题

The fix is in the page section, not in the addPage function

修复在页面部分,而不是在 addPage 函数中

Just before doing an $pdf->addPage You set the flag as addPage automatically calls the header and footer function.

就在执行 $pdf->addPage 之前,您将标志设置为 addPage 自动调用页眉和页脚函数。

Here's the correct code (snippet of PHP script which uses FPDF)

这是正确的代码(使用 FPDF 的 PHP 脚本片段)

/** PHP FPDF */ 
require_once 'classes/FPDF/fpdf.php'; 
require_once 'classes/FPDI/fpdi.php'; 

class PDF extends FPDI 
{ 
     function Header() 
     { 
        if ($this->header == 1)
        {
            $this->SetFont( 'Arial', 'B', 18 ); //set font to Arial, Bold, and 16 Size 

            //create heading with params 
            //0 - 100% width 
            //9 height 
            //"Page Heading" - With this text 
            //1 - border around it, and center aligned 
            //1 - Move pionter to new line after writing this heading 
            //'C' - center aligned 
            $this->Cell( 0, 9, 'Page Heading', 1, 1, 'C' ); 

            $this->ln( 5 ); 
        }
     } 

     function Footer() 
     {
        if ($this->footer == 1)
        {
             //move pionter at the bottom of the page 
             $this->SetY( -15 ); 

             //set font to Arial, Bold, size 10 
             $this->SetFont( 'Arial', 'B', 10 ); 

             //set font color to blue 
             $this->SetTextColor( 52, 98, 185 ); 

             $this->Cell( 0, 10, 'Footer Text', 0, 0, 'L' ); 

             //set font color to gray 
             $this->SetTextColor( 150, 150, 150 ); 

             //write Page No 
             $this->Cell( 0, 10, 'Page No: ' . $this->PageNo(), 0, 0, 'R' ); 
        }
     } 
 } 

// Create new PDF object 
$pdf = new PDF('P','mm','A4'); 

$pdf->header = 0;
$pdf->footer = 0;
$pdf->addPage('','',false); 

// Output pdf file 
$pdf->Output('test.pdf','D'); 

回答by Ogier Schelvis

I know you found out the awnser already for yourself, but as one of the commenters pointed out this didn't work for me with the footer.

我知道您已经为自己找到了 awnser,但正如其中一位评论者指出的那样,这对我的页脚不起作用。

The good news is you can do without setting the external flags. You can use $this->PageNo() to determine whether to include the header and footer or not.

好消息是您无需设置外部标志即可。您可以使用 $this->PageNo() 来确定是否包含页眉和页脚。

For instance if you'd want to exclude the header and footer on the first page, like I did:

例如,如果您想像我一样排除第一页上的页眉和页脚:

function Footer() {
    if($this->PageNo() != 1){
          // footer code
    }
}

If you'd want to let's say exclude them on several pages and not write an endless if statement you should just put the page numbers to exclude in an array and check with in_array() whether the header and/or footer should be included.

如果你想让我们说在几个页面上排除它们而不是写一个无休止的 if 语句,你应该把要排除的页码放在一个数组中,并用 in_array() 检查是否应该包含页眉和/或页脚。

回答by keV

You can define multiple different types of headers and footers by calling functions outside the class:

您可以通过调用类外的函数来定义多种不同类型的页眉和页脚:

class PDF extends FPDF {
    function Header(){
        if(!empty($this->enableheader))
            call_user_func($this->enableheader,$this);
    }
    function Footer(){
        if(!empty($this->enablefooter))
            call_user_func($this->enablefooter,$this);
    }
}


$pdf = new PDF('P');
$pdf->SetTextColor(0);
$pdf->SetFont('Arial','B',10);
$pdf->AliasNbPages();
$pdf->AddPage();

$pdf->Cell(50,6,'Headerless & Footerless page');



$pdf->enableheader = 'header1';
$pdf->AddPage();
$pdf->enablefooter = 'footer1';

$pdf->AddPage();
$pdf->AddPage();


$pdf->enableheader = 'header2';
$pdf->AddPage();
$pdf->enablefooter = 'footer2';

$pdf->Output();



function header1($pdf){
    $pdf->Cell(50,6,'Header type 1',1,0,'L');
}
function footer1($pdf){
    $pdf->SetY(280);
    $pdf->Cell(50,6,'Footer type 1',1,0,'L');
    $pdf->Cell(0,6,"Page: {$pdf->PageNo()} of {nb}",1,0,'R');
}
function header2($pdf){
    $pdf->Cell(50,6,'Header type 2',1,0,'L');
}
function footer2($pdf){
    $pdf->SetY(280);
    $pdf->Cell(50,6,'Footer type 2',1,0,'L');
    $pdf->Cell(0,6,"Page: {$pdf->PageNo()} of {nb}",1,0,'R');
}

The trick to footers is that the footer is added when the next page is created, with the last footer being added when the output is closed. Therefore, you have to define the header before the page is added, and the footer afterwards but before the next page.

页脚的技巧是在创建下一页时添加页脚,并在关闭输出时添加最后一个页脚。因此,您必须在添加页面之前定义页眉,然后在下一页之前定义页脚。