2011-07-29 55 views
1

下面的代碼片段顯示「使用DOMPDF來呈現PDF」。在這裏,我有兩個問題:domPDF CSS問題

1.在哪裏添加CSS代碼?

2.如何製作HTML代碼外部單個代碼(請參閱$ html變量)?

非常感謝您的幫助。

<?php 
require_once("dompdf_config.inc.php"); 
$html = 
    '<html><body>'. 
    '<p>Hello World!</p>'. 
    '</body></html>'; 

$dompdf = new DOMPDF(); 
$dompdf->load_html($html); 

$dompdf->render(); 
$dompdf->stream("hello_world.pdf"); 

?> 
+0

你能澄清問題,使HTML變量與風格的CSS 2?你在問如何檢索不屬於PHP文檔的HTML? – BrianS

回答

3

DOMPDF像瀏覽器一樣讀取HTML。所以,如果你想一些樣式添加到上面的代碼例如,你可以創建一個頭部分:

$html = 
    '<html><head>' . 
    '<style>body { font-family: sans-serif; font-size: 150%; }</style>' . 
    '</head><body>'. 
    '<p>Hello World!</p>'. 
    '</body></html>'; 

或文檔中內嵌:

$html = 
    '<html><body style="font-family: sans-serif; font-size: 150%;">'. 
    '<p>Hello World!</p>'. 
    '</body></html>'; 
0

可以動態使用模板生成一個HTML文件引擎(Smarty)或其他方式。如果使用Smarty,那麼將會生成html代碼,然後使用$ smarty-> fetch(模板文件名),這可用於創建pdf。小心使用CSS,因爲所有的CSS都必須在頁面中提及。

0

海就可以使HTML變量與風格的CSS像的下方,用來生成PDF

<?php 
$html = <<<EOF 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<head> 
    <style> 
    body,html{ 
      margin: 0px; 
      padding: 0px; 
     color:#272727; 
     font-size:14px; 

      } 
     body,html 
     { 
     font-family: 'dejavu sans'; 
     line-height:0.9;!important 
     font-size:14px; 
     } 
</head> 
<body> 
<div> 

</div> 
    </body></html> 
    EOF; 

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

嗨像下面你可以在PDF

<?php 
$html = <<<EOF 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<head> 
    <style> 
    body,html{ 
     color:#272727; 
     font-size:14px; 
     font-family: 'dejavu sans'; 
     line-height:0.9;!important 
     font-size:14px; 
     } 
</head> 
<body> 
<div> 
This is testing content 
</div> 
    </body></html> 
    EOF; 

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

只是一個關於這個問題,考慮到它像一個網頁瀏覽器讀取它可以使用外部樣式表或它只是與內聯的東西? –