2015-08-24 29 views
0

我花了12個小時仍然沒有改進。我嘗試從存儲在服務器中的計算機上顯示不同的圖像,結果很成功。但是當顯示報告餅圖時,每次系統嘗試轉換爲pdf時都不會讀取。它給出了一個空白的pdf文件。另一方面,我可以查看使用echo''創建的餅圖;在reports.php中。我在dompdf文件中使用了相同的概念,但它不起作用。如何使用dompdf-0.5.1將jpgraph轉換爲PDF

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


?> 

回答

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"); 
0

問題是,你基本上要求dompdf從本地文件系統抓取一個名爲「reports-display.php」的圖像文件。當您使用$dompdf->load_html() dompdf不知道內容從哪裏來。 HTML中缺乏完整URL的任何資源引用都通過本地文件系統提取。由於dompdf不解析PHP,源代碼將被讀入,這顯然不是有效的圖像文檔。

您在saving the file locally找到有效的解決方案。還有其他兩種可能性:

1)通過Web服務器指向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();