【问题标题】:FPDF -PHP -Call to undefined methodFPDF -PHP -调用未定义的方法
【发布时间】: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 并离开该行后,它似乎工作正常。感谢您的帮助。无论如何我都会接受答案。 :-)

标签: php arrays class fpdf


【解决方案1】:

您的 PDF 类扩展了 FPDF,因此您需要更改

$pdf = new FPDF();

$pdf = new PDF();

【讨论】:

  • 您好,感谢您的回复。这是一个非常相似的问题的答案之一。但是一旦我改变它就会出现“致命错误:未捕获的异常:FPDF错误:未定义的字体:/Applications/XAMPP/xamppfiles/htdocs中的B ...”
  • 在 fpdf.php 文件中它说:FPDF 类及其版本:1.81 不确定这是否意味着什么。任何的想法?谢谢
  • FPDF 不能使用系统字体,只能使用 FPDF 字体文件夹中的字体。它确实带有一些标准字体,但您必须转换您想要使用的任何其他字体并将它们放在字体文件夹中。
  • 感谢 hcoat,如果您有时间,请查看我的编辑/更新。
猜你喜欢
  • 2012-07-27
  • 1970-01-01
  • 2018-04-11
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多