phpfpdfjpgraph

Cannot extract image into PDF


I am using jpgraph library for charts and FPDF for copying the chart image into PDF. I am able to view the chart on the website but I cannot copy the chart as an image onto the PDF. The error I am receiving is:

Fatal error: Uncaught Exception: FPDF error: Unsupported image type: php in C:\xampp\htdocs\test_db\fpdf\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\test_db\fpdf\fpdf.php(884): FPDF->Error('Unsupported ima...') #1 C:\xampp\htdocs\test_db\generate_pdfTest.php(54): FPDF->Image('http://localhos...', 10, 10, 50.8, 33.866666666667) #2 {main} thrown in C:\xampp\htdocs\test_db\fpdf\fpdf.php on line 271

The code for generating PDF is:


<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');



class PDF extends FPDF
{
// Page header
function Header()
{
    $this->SetFont('Arial','B',14);
    $this->Cell(276, 5, 'Details', 0, 0, 'C');
    $this->Ln();
    $this->SetFont('Times', '', 12);
    $this->Cell(276, 10, 'Details of students', 0, 0, 'C');
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}

}



$pdf=new PDF('P','mm','A4');
$pdf->SetTextColor(44,44,44);
$pdf->Image('http://localhost/Test.php', 10, 10, 25.4/150* 300,  25.4/150*200);
sleep(3);
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);


$pdf->Output("D", "1.pdf");
ob_end_flush(); 

?>

and the code for generating chart is:

<?php // content="text/plain; charset=utf-8"

require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');



$data1y=array(47,80,40,116);
$data2y=array(61,30,82,105);
$data3y=array(115,50,70,93);


// Create the graph. These two calls are always required
$graph = new Graph(350,200,'auto');

$graph->img->SetImgFormat('jpeg');


$graph->SetScale("textlin");

$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);

$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);

$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b2plot = new BarPlot($data2y);
$b3plot = new BarPlot($data3y);

// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
// ...and add it to the graPH
$graph->Add($gbplot);


$b1plot->SetColor("white");
$b1plot->SetFillColor("#cc1111");

$b2plot->SetColor("white");
$b2plot->SetFillColor("#11cccc");

$b3plot->SetColor("white");
$b3plot->SetFillColor("#1111cc");

$graph->title->Set("Bar Plots");

// Display the graph
$graph->Stroke();
?>

Solution

  • Just specify the image type (6th parameter):

    $pdf->Image('http://localhost/Test.php', 10, 10, 25.4/150*300, 25.4/150*200, 'JPG');
    

    Remark: PNG is generally a better choice for charts.