2013-11-27 181 views
2

我已經在Laravel 4FPDF頁眉和頁腳

我使用此代碼嘗試FPDF但功能安裝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; 

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

有人能指出我哪裏出錯?

+0

你應該創建一個新的PDF實例,而不是FPDF。 –

+0

我改變了那部分,但沒有工作。我看到如果我添加$ pdf-> Header();調用工作函數標題。在FPDF的文檔有寫:「這個例子使用的報頭()和頁腳()方法來處理頁面的頁眉和頁腳他們被自動調用的,他們已經存在於FPDF類,但什麼都不做,所以我們必須。擴展課程並覆蓋它們。「 – chianta

回答

11

寫延伸FPDF一個新的類,並把你的頭&頁腳方法在那裏。

class PDF extends FPDF { 

    function Header() {} 
    function Footer() {} 
} 
在控制器

則...

new PDF();