Trevor Ackermann
Trevor Ackermann

Reputation: 176

FPDF Error Cannot redeclare class FPDF

I am receiving the following error when using FPDF

Cannot redeclare class FPDF in /home/www/etrackbureau.co.za/fpdf.php on line 12

I am not declaring other classes in any other section of the code

<?php
//include_once('diag.php');
include_once('mysql_table.php');
include_once('../../fpdf.php');


class PDF extends PDF_MySQL_Table
{
function Header()
{
    //Title
    $this->SetFont('Arial','',18);
    $this->Cell(0,6,'World populations',0,1,'C');
    $this->Ln(10);
    //Ensure table header is output
    parent::Header();
}
}

//Connect to database
mysql_connect('localhost','stingin_assist','trevor74');
mysql_select_db('stingin_assist');

$pdf=new PDF();
$pdf->AddPage();
//First table: put all columns automatically
$pdf->Table('select * from bureau order by rep_date');
$pdf->AddPage();
//Second table: specify 3 columns
$pdf->AddCol('rank',20,'','C');
$pdf->AddCol('name',40,'Country');
$pdf->AddCol('pop',40,'Pop (2001)','R');
$prop=array('HeaderColor'=>array(255,150,100),
            'color1'=>array(210,245,255),
            'color2'=>array(255,255,210),
            'padding'=>2);
$pdf->Table('select name, format(pop,0) as pop, rank from country order by rank limit 0,10',$prop);

$pdf->Output();
?>

I am declaring the class as far as I know only once. As I am new to FPDF a second question is this best to create PDF files with Graphs and writing in it. I have searched the forums and numerous has been mentioned. I am looking for a base line PDF system to write reports from information out of my DB and placing them into graphs

Upvotes: 0

Views: 5200

Answers (1)

Remon Huijts
Remon Huijts

Reputation: 1001

You are combining several scripts from the FPDF website but each of them has this code somewhere at the top of the file:

require('fpdf.php');

For example, the code inside diag.php will require sector.php and that file will require fpdf.php. The code inside mysql_table.php will also require fpdf.php. This means fpdf.php is included more than once and thus the class FPDF is declared more than once, which results in the error you got. Change the line in all the files to:

require_once('fpdf.php');

The example scripts on the FPDF website show nicely what is possible. To actually combine the functionality of these different scripts, you might need to cut and paste functions from those scripts into one big class or change who extends what, to get multilevel (not multiple) inheritance.

Upvotes: 1

Related Questions