简体   繁体   中英

FPDF -PHP -Call to undefined method

I have been stuck on trying to print an array of data into a table in fpdf. This is the tutorial I was following: http://www.fpdf.org/en/tutorial/tuto5.htm

Except it keeps saying:

Fatal error: Uncaught Error: Call to undefined method FPDF::FancyTable() in ...

I have seen many other questions like this but none of them are fixing mine.

I feel like this should be a really easy fix and I'm doing something dumb.

Any help would be greatly appreciated.

<?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();

?>

**UPDATE **
-Thanks for the help. I did change the font and made no difference. I changed it from:

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

to

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

(In the fancy table function)

But it comes up with heaps of these errors:

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...

I'm sorry, but I don't really know what to do.

However, I got some of the table to display by removing the lines with number_format. IE I removed these two lines:

  $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);

Any reason why? Its displaying the first two columns but not the rest of it.

Your PDF class extends FPDF so you need to change

$pdf = new FPDF();

to

$pdf = new PDF();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM