FPDF -PHP -调用未定义的方法
FPDF -PHP -Call to undefined method
我一直在尝试将一组数据打印到 fpdf 中的 table。
这是我正在关注的教程:
http://www.fpdf.org/en/tutorial/tuto5.htm
除了它一直在说:
Fatal error: Uncaught Error: Call to undefined method FPDF::FancyTable() in ...
我见过很多其他类似的问题,但 none 正在解决我的问题。
我觉得这应该是一个非常简单的修复方法,但我正在做一些愚蠢的事情。
如有任何帮助,我们将不胜感激。
<?php
require('../fpdf/fpdf.php');
class PDF extends FPDF
{
// Colored table
function FancyTable($header, $data)
{
// Colors, line width and bold font
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
// Header
$w = array(40, 35, 40, 45);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
// Color and font restoration
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = false;
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
$this->Ln();
$fill = !$fill;
}
// Closing line
$this->Cell(array_sum($w),0,'','T');
}
}
$csv = array();
$lines = file('../data/StaffData.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$csv[$key] = str_getcsv($value);
}
$pdf = new FPDF();
// Column headings
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. ');
$data = $csv;
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>
**更新**
-谢谢您的帮助。
我确实更改了字体,但没有任何区别。我将其更改为:
$this->SetFont('','B');
到
$this->SetFont('Arial','',14);
(在花哨的table函数中)
但是它出现了很多这样的错误:
Warning: number_format() expects parameter 1 to be float, string given in /Applications/XAMPP/xamppfiles...
Notice: A non well formed numeric value encountered in /Applications/XAMPP/xamppfiles...
Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file in /Applications/XAMPP/xamppfiles...
对不起,我真的不知道该怎么办。
但是,通过删除 number_format 的行,我得到了一些要显示的 table。即我删除了这两行:
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
有什么原因吗?它显示前两列,但不显示其余部分。
您的 PDF class 扩展了 FPDF,因此您需要更改
$pdf = new FPDF();
到
$pdf = new PDF();
我一直在尝试将一组数据打印到 fpdf 中的 table。 这是我正在关注的教程: http://www.fpdf.org/en/tutorial/tuto5.htm
除了它一直在说:
Fatal error: Uncaught Error: Call to undefined method FPDF::FancyTable() in ...
我见过很多其他类似的问题,但 none 正在解决我的问题。
我觉得这应该是一个非常简单的修复方法,但我正在做一些愚蠢的事情。
如有任何帮助,我们将不胜感激。
<?php
require('../fpdf/fpdf.php');
class PDF extends FPDF
{
// Colored table
function FancyTable($header, $data)
{
// Colors, line width and bold font
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
// Header
$w = array(40, 35, 40, 45);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
// Color and font restoration
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = false;
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
$this->Ln();
$fill = !$fill;
}
// Closing line
$this->Cell(array_sum($w),0,'','T');
}
}
$csv = array();
$lines = file('../data/StaffData.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$csv[$key] = str_getcsv($value);
}
$pdf = new FPDF();
// Column headings
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. ');
$data = $csv;
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>
**更新**
-谢谢您的帮助。
我确实更改了字体,但没有任何区别。我将其更改为:
$this->SetFont('','B');
到
$this->SetFont('Arial','',14);
(在花哨的table函数中)
但是它出现了很多这样的错误:
Warning: number_format() expects parameter 1 to be float, string given in /Applications/XAMPP/xamppfiles...
Notice: A non well formed numeric value encountered in /Applications/XAMPP/xamppfiles...
Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file in /Applications/XAMPP/xamppfiles...
对不起,我真的不知道该怎么办。
但是,通过删除 number_format 的行,我得到了一些要显示的 table。即我删除了这两行:
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
有什么原因吗?它显示前两列,但不显示其余部分。
您的 PDF class 扩展了 FPDF,因此您需要更改
$pdf = new FPDF();
到
$pdf = new PDF();