【问题标题】:domPDF CSS problemdomPDF CSS 问题
【发布时间】:2013-05-27 17:34:39
【问题描述】:

以下代码 sn-p 显示为“使用 DOMPDF 呈现 PDF”。在这里,我有两个问题:

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

?>

【问题讨论】:

  • 你能澄清问题2吗?您是在问如何检索不属于 PHP 文档的 HTML?

标签: php dompdf


【解决方案1】:

DOMPDF 像浏览器一样读取 HTML。因此,如果您想在上面的代码示例中添加一些样式,您可以创建一个 head 部分:

$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>';

【讨论】:

    【解决方案2】:

    您可以使用模板引擎(Smarty)或其他方式动态生成 HTML 文件。如果使用 Smarty,则 $smarty->fetch(template file name) 将生成 html 代码,这可用于创建 pdf。使用 CSS 时要小心,因为页面中必须提到所有的 CSS。

    【讨论】:

      【解决方案3】:

      嘿,你可以像下面这样使用样式 css 制作 html 变量并用来生成 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");
              ?>
      

      【讨论】:

        【解决方案4】:

        您好,如下所示,您可以在 pdf 中使用样式 css 制作 html 变量

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

        【讨论】:

        • 只是一个关于这个的问题,考虑到它像网络浏览器一样读取它,我可以使用外部样式表还是只使用内联的东西?
        猜你喜欢
        • 2011-01-20
        • 1970-01-01
        • 2016-07-17
        • 2016-10-17
        • 2015-10-22
        • 2018-08-05
        • 1970-01-01
        • 2013-05-22
        • 2012-11-30
        相关资源
        最近更新 更多