无法将图像提取为 PDF

Cannot extract image into PDF

我正在使用 jpgraph 图表库和 FPDF 将图表图像复制到 PDF 中。我可以在网站上查看图表,但无法将图表作为图像复制到 PDF 中。我收到的错误是:

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

生成PDF的代码为:


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

?>

生成图表的代码是:

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

只需指定图像类型(第6个参数):

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

备注:PNG 通常是图表的更好选择。