2014-02-06 44 views
0

我使用dompdf嘗試將html轉換爲pdf文件。該文件創建沒有問題,但當我試圖打開該文件時,它已損壞。當我在記事本中打開文件時,我可以看到它只是原始的html。所以它沒有任何轉換,它只是把它放在一個擴展名爲pdf的文件中。dompdf沒有正確創建pdf

這裏是我的代碼:

include_once '/files/dompdf/dompdf_config.inc.php'; 
$files = glob("/files/dompdf/include/*.php"); 
foreach($files as $file) include_once($file); 

ob_start(); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title> 
      Title 
     </title> 

    </head> 
    <body> 
     <div><p>Hello World</p></div> 
    </body> 

</html> 

<?php 
$html = ob_get_clean(); 

$dompdf = new DOMPDF(); 
$dompdf->load_html($html); 
$dompdf->render(); 
$dompdf->stream("sample.pdf"); 

這只是抓住,正是因爲它寫在這裏的HTML並將其保存爲sample.pdf但它不是一個正確的PDF文件。我錯過了什麼嗎?

+0

僅供參考,除非你已經禁用你不需要線2磁帶自動加載機 - 3 DOMPDF設置了一個自動加載需要獲取的文件。 – BrianS

回答

0

我通過剝離斜線修復了它。這是更新:

$html = ob_get_clean(); 

if (get_magic_quotes_gpc()) 
    $html = stripslashes($html); 

$dompdf = new DOMPDF(); 
$dompdf->load_html($html); 
$dompdf->render(); 
$dompdf->stream("sample.pdf"); 
+0

我不確定爲什麼加入'stripslashes()'可以解決你的問題,但是如果這樣做... ... – BrianS