laravel FPDF 页眉和页脚

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

FPDF Header and Footer

phplaravellaravel-4fpdf

提问by chianta

I've install FPDF on Laravel 4.

我已经在 Laravel 4 上安装了 FPDF。

I use this code to try FPDF but the functions Header()and Footer()don't work

我使用此代码尝试 FPDF 但功能Header()Footer()不起作用

class PDF extends Fpdf
{
    // Page header
    function Header()
    {
        // Logo
        $this->Image('logo.png',10,6,30);

        // Arial bold 15
        $this->SetFont('Arial','B',15);

        // Move to the right
        $this->Cell(80);

        // Title
        $this->Cell(30,10,'Title',1,0,'C');

        // Line break
        $this->Ln(20);

    }

    // Page footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);

        // Arial italic 8
        $this->SetFont('Arial','I',8);

        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
}


// Instanciation of inherited class

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);

for($i=1;$i<=40;$i++)
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);

$pdf->Output();
exit;

This is the result: https://dl.dropboxusercontent.com/u/70804756/doc%20(3).pdf

这是结果:https: //dl.dropboxusercontent.com/u/70804756/doc%20(3).pdf

Can someone point me where the error is?

有人可以指出我的错误在哪里吗?

回答by bencarter78

Write a new class which extends FPDF and put your Header & Footer methods in there.

编写一个扩展 FPDF 的新类并将您的页眉和页脚方法放在那里。

class PDF extends FPDF {

  function Header() {}
  function Footer() {}
}

Then in your controller...

然后在你的控制器中......

new PDF();