无法将图像提取为 PDF
Posted
技术标签:
【中文标题】无法将图像提取为 PDF【英文标题】:Cannot extract image into PDF 【发布时间】:2020-08-07 00:16:51 【问题描述】:我使用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();
?>
【问题讨论】:
【参考方案1】:只需指定图片类型(第6个参数):
$pdf->Image('http://localhost/Test.php', 10, 10, 25.4/150*300, 25.4/150*200, 'JPG');
备注:PNG 通常是图表的更好选择。
【讨论】:
您好,谢谢您的回复。我尝试使用 JPG/PNG,但最终得到了这个错误。如果我将其更改为 JPG ``` Uncaught Exception: FPDF error: Missing or wrong image file``` 如果我将其更改为 PNGFatal error: Uncaught Exception: FPDF error: Not a PNG file: Test.php
声明的类型必须与真实的类型匹配。您的脚本会创建一个 JPEG。如果您想要 PNG,请将调用更改为 SetImgFormat()
。
是的,我确实改变了两边,但我收到了错误。
错误Fatal error: Uncaught Exception: FPDF error: Missing or incorrect image file: Test.php in C:\xampp\htdocs\test_db\fpdf\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\test_db\fpdf\fpdf.php(1242): FPDF->Error('Missing or inco...') #1 C:\xampp\htdocs\test_db\fpdf\fpdf.php(885): FPDF->_parsejpg('Test.php') #2 C:\xampp\htdocs\test_db\generate_pdfTest.php(63): FPDF->Image('Test.php', 10, 10, 50.8, 33.866666666667, 'jpg') #3 main thrown in C:\xampp\htdocs\test_db\fpdf\fpdf.php on line 271
从消息中,您似乎删除了 URL,只保留了文件名...以上是关于无法将图像提取为 PDF的主要内容,如果未能解决你的问题,请参考以下文章