我正在嘗試使用laravel 5.2生成pdf文件。我已成功將fpdf安裝到我的laravel,因爲它可以生成一個簡單的pdf。該代碼是這樣的:Laravel中的FPDF 5.2
<?php
namespace App\Helpers;
use Fpdf;
class ReportHelper{
public static $elementPerPage = 10;
public static function generatePDF()
{
Fpdf::AddPage();
Fpdf::SetFont('Arial','B',16);
Fpdf::Cell(40,10,'Hello World!');
Fpdf::Output();
exit;
}
}
?>
然後,我從以下在http://www.fpdf.org/教程2.例如,如果我們想要一個新的頁眉或頁腳,我們需要這樣的代碼:
<?php
require('fpdf.php');
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();
?>
所以,我創建裏面我reportHelper文件{類是低於ReportHelper}這樣的一個新的類:
class PDF extends Fpdf
{
//Header
function Header()
{
//Logo
$this->Image('mangekyo2.JPG',18,6,25,20,'JPG'); //posisi (x,y), image size
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(70); //title position
// Title
$this->Cell(60,10,'Hisoka PDF Title',1,0,'C');//box size
// 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');
}
}
然後在generatePDF方法{ReportHelper類裏面},我改變了代碼變成了這個樣子:
class ReportHelper{
public static $elementPerPage = 10;
public static function generatePDF()
{
// Fpdf::AddPage();
// Fpdf::SetFont('Arial','B',16);
// Fpdf::Cell(40,10,'Hello World!');
// Fpdf::Output();
// exit;
//Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=60;$i++){
$pdf->Cell(0,10,'Printing line number '.$i,0,1);
if($i == 10 || $i== 20 || $i == 30) {
$pdf->Cell(0,10,'Hisokaline... '.$i,0,1);
$pdf->Cell(0,10,'X : '.$pdf->GetX().', Y : '.$pdf->GetY(),0,1);
$pdf->Image('mangekyo2.JPG',48,$pdf->GetY(),25,20,'JPG');
}
}
$pdf->Output();
}
}
當我瀏覽/運行這個頁面,它給出了一個錯誤
Call to undefined method App\Helpers\PDF::AliasNbPages()
我知道AliasNbPages()是FPDF的方法,但爲什麼就不能承認?它不只是AliasNbPages(),但AddPages和SetFont也是未定義的,有沒有一種方法可以在laravel中調用這個方法? 我試圖
public static function generatePDF()
{
Fpdf::Header()
{
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(70); //title position
// Title
$this->Cell(60,10,'Hisoka PDF Title',1,0,'C');//box size
// Line break
$this->Ln(20);
};
}
它不工作太...