【发布时间】:2016-07-23 03:56:03
【问题描述】:
我一直在尝试将一组数据打印到 fpdf 中的表格中。 这是我正在关注的教程: http://www.fpdf.org/en/tutorial/tuto5.htm
除了它一直说:
致命错误:未捕获的错误:调用未定义的方法 FPDF::FancyTable() in ...
我见过很多其他类似的问题,但没有一个能解决我的问题。
我觉得这应该是一个非常容易解决的问题,我正在做一些愚蠢的事情。
任何帮助将不胜感激。
<?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);
(在花式表函数中)
但它会出现大量这些错误:
警告:number_format() 期望参数 1 为浮点数,/Applications/XAMPP/xamppfiles 中给出的字符串...
注意:在 /Applications/XAMPP/xamppfiles...中遇到格式不正确的数值...
致命错误:未捕获的异常:FPDF 错误:一些数据已输出,无法在 /Applications/XAMPP/xamppfiles 中发送 PDF 文件...
对不起,我真的不知道该怎么办。
但是,我通过删除带有 number_format 的行来显示一些表格。 I.E 我删除了这两行:
$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);
有什么理由吗?它显示前两列,但不显示其余部分。
【问题讨论】:
-
错误告诉您
$row[2]和$row[3]是字符串而不是数字,因此您不能使用number_format,除非您先将它们从字符串转换。 -
哦,好的。我回应了它们,行将所有数据保存在一个字符串中。如何将字符串转换为数字?删除 number_format 并离开该行后,它似乎工作正常。感谢您的帮助。无论如何我都会接受答案。 :-)