如何使用dompdf-0.5.1将jpgraph放入PDF

时间:2015-08-24 10:48:24

标签: php pdf dompdf jpgraph

我花了12个小时仍然没有改善。我尝试从存储在服务器中的计算机显示不同的图像,结果是成功的。但是,当显示报告饼图时,每次系统尝试转换为pdf时都不会读取。它给出了一个空白的pdf文件。另一方面,我可以使用echo&#39 ;;查看我创建的饼图。在reports.php中。我在dompdf文件中使用了相同的概念,但它不起作用。

DOMPDF     

<html>
<head>
<title></title>
</head>
<body>
<?php echo'<img src="reports-display.php"/>';?>
</body>
</html>
<?php
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

JPGRAPH绘图

<?php   
require('dbcon.php');
require_once('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_pie.php');
require_once ('jpgraph/src/jpgraph_pie3d.php');


//LEGEND
//YELLOW=LIVE BLUE=WAITING GREEN=DONE


//sql query for live
$live = mysql_query("Select count(*) as count1 from tbl_display_ads where status LIKE '%Live%'") or die(mysql_error());


//sql query for waiting
$waiting = mysql_query("Select count(*) as count2 from tbl_display_ads where status LIKE '%Waiting%'") or die(mysql_error());


//sql query for done/posted advertisement
$done = mysql_query("Select count(*) as count3 from tbl_display_ads where status LIKE '%Done%'") or die(mysql_error());

//While loop for live
while($resultlive = mysql_fetch_array($live)) 
{ 
    $totallive = $resultlive['count1']; 
} 

//While loop for waiting
while($resultwaiting = mysql_fetch_array($waiting)) 
{ 
    $totalwaiting = $resultwaiting['count2']; 
}

//While loop for done
while($resultdone = mysql_fetch_array($done)) 
{ 
    $totaldone = $resultdone['count3']; 
}

// Some data
$data = array($totallive,$totalwaiting,$totaldone);

// Create the Pie Graph. 

$graph = new PieGraph(500,450);

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

// Set A title for the plot
$graph->title->Set("Figure 1.1: Totality of Display Advertisement");

// Create
$p1 = new PiePlot3D($data); 
$p1->SetCenter(0.5,0.55);

$p1->SetLegends(array("Live","Waiting","Done"));
$graph->legend->SetPos(0.5,0.100,'center','bottom');
$graph->Add($p1);
$p1->ShowBorder();
$p1->SetColor('black');
$p1->ExplodeSlice(1);
$graph->Stroke();

// Get the handler to prevent the library from sending the
// image to the browser
$gdImgHandler = $graph->Stroke(_IMG_HANDLER);

// Stroke image to a file and browser

// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);

// Send it back to browser
$graph->img->Headers();
$graph->img->Stream();


?>

2 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。在report-display.php中,我将图形的扩展名设置为.png,并保存到报告的目录文件夹中。

 DEFINE("DEFAULT_GFORMAT","auto");
$graph->img->SetImgFormat("png");
if(file_exists("Reports/reports-display.png")) unlink("Reports/reports-display.png");
$graph->Stroke("Reports/reports-display.png");

答案 1 :(得分:0)

问题是你基本上要求dompdf从本地文件系统中获取名为“reports-display.php”的图像文件。当您使用$dompdf->load_html()时,dompdf不知道内容来自何处。 HTML中缺少完整URL的任何资源引用都通过本地文件系统引入。由于dompdf不解析PHP,因此将读入源代码,这显然不是有效的图像文档。

您在saving the file locally找到了有效的解决方案。还有另外两种可能性:

1)通过网络服务器指向jpgraph脚本。

<html>
<head>
  <title></title>
</head>
<body>
  <img src="http://example.com/reports-display.php"/>
</body>
</html>

2)捕获jpgraph输出并以data-uri插入文档。

<html>
<head>
  <title></title>
</head>
<body>
  <img src="data:image/png;base64,<?php echo base64_encode(include_once('reports-display.php');?>"/>
</body>
</html>

使用此方法,必须更新reports-deplay.php以返回图像而不是流式传输图像。类似的东西:

$graph = new PieGraph(500,450);

// snip steps that generate the content

return $graph->Stroke();