2016-07-23 88 views
0

我一直试图将一个数据数组打印到fpdf的表中。 这是我继教程: http://www.fpdf.org/en/tutorial/tuto5.htmFPDF -PHP -Call to undefined method

除了它口口声声说:

Fatal error: Uncaught Error: Call to undefined method 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(); 

?> 

**更新**
-Thanks的帮助。 我确实改变了字体并没有改变。我改变它:

$this->SetFont('','B'); 

$this->SetFont('Arial','',14); 

(在花式球台功能)

但这些错误的堆出现:

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行来显示一些表格。 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); 

任何原因为什么?它显示前两列,但不显示其余部分。

+0

错误是告诉你'$行[2]'和'$行[3]'是扩展FPDF字符串而不是数字,所以你不能使用'number_format',除非你先将它们从一个字符串中转换出来。 – hcoat

+0

噢,好的。我呼应它们,行将所有数据保存在一个字符串中。我将如何将字符串转换为数字?在删除number_format并离开行之后,它好像工作正常。 感谢您的帮助hcoat。无论如何,我会接受答案。 :-) –

回答

1

您的PDF类,所以你需要改变

$pdf = new FPDF(); 

$pdf = new PDF(); 
+0

嗨,谢谢你的回复。这是一个非常类似问题的答案之一。但是一旦我改变它出现“致命错误:未捕获的异常:FPDF错误:未定义字体:B在/ Applications/XAMPP/xamppfiles/htdocs ...” –

+0

在fpdf.php文件中它说: class FPDF 和它的版本:1.81 不知道这是否意味着什么。任何想法? 谢谢 –

+0

FPDF不能使用系统字体,只能使用FPDF字体文件夹中的字体。它带有一些标准字体,但您必须转换您想要使用的任何其他字体并将它们放在字体文件夹中。 – hcoat

相关问题