tags:

views:

486

answers:

2

The server information says that the PHP version is 5.2.10. Our fPDF works on version 5.3, but not version 5.2.8.

The error is always

FPDF error: Missing or incorrect image file:

What is the problem?

Code Snippet:

<?php
require('fpdf.php');

class PDF extends FPDF
{
var $aCols=array();
function Header()
{
    //Logo
    $this->Image('UST-LOGO.jpg',25,10,18);
    //$this->Ln(5);
    //$this->Cell(0,10,$date);
    $this->SetFont('Arial','B',12);
    //Move to the right
    //$this->Cell(80);
    //Title
    $this->Cell(0,10,'University of Santo Tomas',0,0,'C');
    $this->Ln(5);
    $this->SetFont('Arial','I',12);
    $this->Cell(0,10,'Office for Student Affairs',0,0,'C');
    $this->Ln(5);
    $this->Cell(0,10,'Annual Report',0,0,'C');
    $this->Ln(15);
}
function TableHeader()
{
    $this->SetFont('Arial','B',12);
    $this->SetX($this->TableX);
    $fill=!empty($this->HeaderColor);
    if($fill)
        $this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],
                                $this->HeaderColor[2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],6,$col['c'],1,0,'C',$fill);
    $this->Ln();
}

function Row($data)
{
    $this->SetX($this->TableX);
    $ci=$this->ColorIndex;
    $fill=!empty($this->RowColors[$ci]);
    if($fill)
        $this->SetFillColor($this->RowColors[$ci][0],
        $this->RowColors[$ci][1],$this->RowColors[$ci][2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);
    $this->Ln();
    $this->ColorIndex=1-$ci;
}
function CalcWidths($width,$align)
{
    //Compute the widths of the columns
    $TableWidth=0;
    foreach($this->aCols as $i=>$col)
    {
        $w=$col['w'];
        if($w==-1)
            $w=$width/count($this->aCols);
        elseif(substr($w,-1)=='%')
            $w=$w/100*$width;
        $this->aCols[$i]['w']=$w;
        $TableWidth+=$w;
    }
    //Compute the abscissa of the table
    if($align=='C')
        $this->TableX=max(($this->w-$TableWidth)/2,0);
    elseif($align=='R')
        $this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
    else
        $this->TableX=$this->lMargin;
}
function AddCol($field=-1,$width=-1,$caption='',$align='L')
{
    //Add a column to the table
    if($field==-1)
        $field=count($this->aCols);
    $this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align);
}
function Table($query,$prop=array())
{
    //Issue query
    $res=mysql_query($query) or die
    ('Error: '.mysql_error()."<BR>Query: $query");
    //Add all columns if none was specified
    if(count($this->aCols)==0)
    {
        $nb=mysql_num_fields($res);
        for($i=0;$i<$nb;$i++)
            $this->AddCol();
    }
    //Retrieve column names when not specified
    foreach($this->aCols as $i=>$col)
    {
        if($col['c']=='')
        {
            if(is_string($col['f']))
                $this->aCols[$i]['c']=ucfirst($col['f']);
            else
                $this->aCols[$i]['c']=ucfirst
                       (mysql_field_name($res,$col['f']));
        }
    }
    //Handle properties
    if(!isset($prop['width']))
        $prop['width']=0;
    if($prop['width']==0)
        $prop['width']=$this->w-$this->lMargin-$this->rMargin;
    if(!isset($prop['align']))
        $prop['align']='C';
    if(!isset($prop['padding']))
        $prop['padding']=$this->cMargin;
    $cMargin=$this->cMargin;
    $this->cMargin=$prop['padding'];
    if(!isset($prop['HeaderColor']))
        $prop['HeaderColor']=array();
    $this->HeaderColor=$prop['HeaderColor'];
    if(!isset($prop['color1']))
        $prop['color1']=array();
    if(!isset($prop['color2']))
        $prop['color2']=array();
    $this->RowColors=array($prop['color1'],$prop['color2']);
    //Compute column widths
    $this->CalcWidths($prop['width'],$prop['align']);
    //Print header
    $this->TableHeader();
    //Print rows
    $this->SetFont('Arial','',11);
    $this->ColorIndex=0;
    $this->ProcessingTable=true;
    while($row=mysql_fetch_array($res))
        $this->Row($row);
    $this->ProcessingTable=false;
    $this->cMargin=$cMargin;
    $this->aCols=array();
}

function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    $this->SetFont('Arial','I',8);
    $this->Cell(0,10,'Generated by ExAct: Expert System 
                for Activity Proposals',0,0,'L');
    $this->Ln(5);
    $this->Cell(0,10,$date);
}
}

include("phpgraphlib.php");
include("phpgraphlib_pie.php");
include("connection.php");
/*$id=$_GET['id'];
     $result=mysql_query("select * from approved where id='$id'");
     $row=mysql_fetch_assoc($result);
     $_SESSION['other_id']=$id;
     $id=$_SESSION['other_id'];*/
$querypa = "SELECT COUNT(nature) FROM approved WHERE nature='Parade'";
$resultpa = mysql_query($querypa);
$printpa = mysql_result($resultpa,0);
$querycs = "SELECT COUNT(nature) FROM approved WHERE 
                         nature='Community Service'";
$resultcs = mysql_query($querycs);
$printcs = mysql_result($resultcs,0);
$queryga = "SELECT COUNT(nature) FROM approved WHERE 
                         nature='General Assembly'";
$resultga = mysql_query($queryga);
$printga = mysql_result($resultga,0);
$querypl = "SELECT COUNT(nature) FROM approved WHERE 
            nature='Play/Showcase/Socio-Cultural Show/Film Showing'";
$resultpl = mysql_query($querypl);
$printpl = mysql_result($resultpl,0);
$queryco = "SELECT COUNT(nature) FROM approved WHERE 
                         nature='Competition/Sportsfest'";
$resultco = mysql_query($queryco);
$printco = mysql_result($resultco,0);
$queryfr = "SELECT COUNT(nature) FROM approved WHERE 
                         nature='Fund Raising'";
$resultfr = mysql_query($queryfr);
$printfr = mysql_result($resultfr,0);
$queryse = "SELECT COUNT(nature) FROM approved WHERE 
                         nature='Seminar/Convention/Conference/Training'";
$resultse = mysql_query($queryse);
$printse = mysql_result($resultse,0);


$totalAct=$printpa+$printga+$printpl+$printcs+$printco+$printfr+$printse;
$avepa=($printpa/$totalAct)*100;
$avecs=($printcs/$totalAct)*100;
$avega=($printga/$totalAct)*100;
$avepl=($printpl/$totalAct)*100;
$aveco=($printco/$totalAct)*100;
$avefr=($printfr/$totalAct)*100;
$avese=($printse/$totalAct)*100;
$pa=number_format($avepa,2);
$cs=number_format($avecs,2);
$ga=number_format($avega,2);
$pl=number_format($avepl,2);
$co=number_format($aveco,2);
$fr=number_format($avefr,2);
$se=number_format($avese,2);

//$querydb="INSERT INTO report(nature,totalAct) 
                     values('Parade','$pa'),('General //Assembly','$ga'),
                           ('Play','$pl'),('Community Service','$cs')";
//$resultdb = mysql_query($querydb);

$date=date("m-d-Y");


$url = 'http://exact.byethost10.com/sample_pie.php';
$img = 'C:/xampp/htdocs/EXACT/piechart.jpg';
file_put_contents($img, file_get_contents($url));

$pdf=new PDF('L','mm','Letter');
$pdf->SetRightMargin(20);
$pdf->SetLeftMargin(20);
$pdf->AddPage();
$pdf->SetFont('Arial','',12);

//Second table: specify 3 columns
$pdf->Cell(0,10,$date);
$pdf->Ln(10);
$pdf->AddCol('org',52,'org','L');
$pdf->AddCol('title',83,'title','L');
$pdf->AddCol('nature',92,'','C');
$pdf->AddCol('start_date',25,'start_date','L');
$pdf->AddCol('end_date',25,'end_date','L');
$prop=array('HeaderColor'=>array(255,150,100),
            //'color1'=>array(210,245,255),
            'color2'=>array(255,255,210),
            'padding'=>2);
$pdf->Table('select org, title, nature, start_date, 
             end_date from approved order by id',$prop);

$pdf->Cell(0,10,'Parade:');
$pdf->SetX(110);$pdf->Cell(0,10,$printpa);
$pdf->SetX(120);$pdf->Cell(0,10,$pa);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(5);
$pdf->Cell(0,10,'Community Service:');
$pdf->SetX(110);$pdf->Cell(0,10,$printcs);
$pdf->SetX(120);$pdf->Cell(0,10,$cs);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(5);
$pdf->Cell(0,10,'General Assembly:');
$pdf->SetX(110);$pdf->Cell(0,10,$printga);
$pdf->SetX(120);$pdf->Cell(0,10,$ga);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(5);
$pdf->Cell(0,10,'Play/Showcase/Socio-Cultural Show/Film Showing:');
$pdf->SetX(110);$pdf->Cell(0,10,$printpl);
$pdf->SetX(120);$pdf->Cell(0,10,$pl);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(5);
$pdf->Cell(0,10,'Competition/Sportsfest:');
$pdf->SetX(110);$pdf->Cell(0,10,$printco);
$pdf->SetX(120);$pdf->Cell(0,10,$co);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(5);
$pdf->Cell(0,10,'Fund Raising:');
$pdf->SetX(110);$pdf->Cell(0,10,$printfr);
$pdf->SetX(120);$pdf->Cell(0,10,$fr);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(5);
$pdf->Cell(0,10,'Seminar/Convention/Conference/Training:');
$pdf->SetX(110);$pdf->Cell(0,10,$printse);
$pdf->SetX(120);$pdf->Cell(0,10,$se);
$pdf->SetX(130);$pdf->Cell(0,10,'%');
$pdf->Ln(15);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,10,'Total number of Activities:');
$pdf->SetX(110);$pdf->Cell(0,10,$totalAct);
$pdf->SetX(120);$pdf->Cell(0,10,' 100  %');
$pdf->Ln(15);
$pdf->SetX(65);
$pdf->Image($file=$img, $x=null, $y=null, 
           $w=150, $h=100, $type='jpeg', $link='');
$pdf->Output();

?>
A: 

The first thing that jumps out is:

$pdf->Image($file=$img, $x=null, $y=null, $w=150, $h=100, $type='jpeg', $link='');

near the end. Use:

$pdf->Image($img, null, null, 150, 100, "jpeg", "");

See if this solves it.

richsage
those two lines are functionally equivalent, it's just that the first one also assigns the values to some (unused) variables
nickf
ahh of course, duh... it's getting too late here, clearly :-)
richsage
+1  A: 

It might be this line:

$this->Image('UST-LOGO.jpg',25,10,18);

Comment that line out and try it again. If it now works, you've probably got the path wrong. I've come into the habit of specifying all my files "absolutely" by using the magic "constant" __FILE__.

If the UST-LOGO is in the same directory as that file you posted the source of, then you could change your code to this:

$this->Image(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'UST-LOGO.jpg',25,10,18);


edit: Here is the code from the FPDF class which raises that error:

// line 1199
$a=GetImageSize($file);
if(!$a)
    $this->Error('Missing or incorrect image file: '.$file);

The error can only be caused by the file either not existing (the path is wrong) or it can't read the image data (the file is corrupt or file permissions are blocking it)

I would suggest adding a line in your code (or in the FPDF code) checking file_exists($file). If that returns false, then take a closer look at your directories (on the server!). If that returns true, then perhaps try replacing the image with another one to see if that works.

nickf
i did this but it's still the same. FPDF error: Missing or incorrect image file: C:\xampp\htdocs\exact\piechart.jpg
noob
there *is* a file there, right?
nickf
yes that file is there
noob