php 如何在dom pdf中包含外部样式表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19805148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:03:35  来源:igfitidea点击:

How to include the external style sheet in dom pdf

phpdompdf

提问by Kiran

I am using Dompdf for the report generation in the php. I am not able to include the external style sheet for the same...

我在 php 中使用 Dompdf 生成报告。我无法包含相同的外部样式表...

The code I am using is similar to the following:

我使用的代码类似于以下内容:

<?php
require_once "dompdf/dompdf_config.inc.php";
$dompdf = new DOMPDF();

$html ="
<table>
  <tr >
    <td class='abc'>testing table</td>
    </tr>
  <tr>
   <td class='def'>Testng header</td>
  </tr>
</table>";

$dompdf->load_html($html);
$dompdf->render();
$dompdf->set_base_path('localhost/exampls/style.css');
$dompdf->stream("hello.pdf");
?>

Please let me know how to include the external css file..

请让我知道如何包含外部 css 文件..

回答by BrianS

$dompdf->set_base_path()isn't where you specify your CSS. That method gives you the opportunity to define a base path that is appended to relative paths in the document.

$dompdf->set_base_path()不是您指定 CSS 的地方。该方法使您有机会定义附加到文档中的相对路径的基本路径。

Your CSS should be part of the HTML you feed to dompdf. Something like the following:

您的 CSS 应该是您提供给 dompdf 的 HTML 的一部分。类似于以下内容:

<?php
require_once "dompdf/dompdf_config.inc.php";
$dompdf = new DOMPDF();

$html ="
<html>
<head>
<link type="text/css" href="localhost/exampls/style.css" rel="stylesheet" />
</head>
<body>
<table>
  <tr >
    <td class='abc'>testing table</td>
    </tr>
  <tr>
   <td class='def'>Testng header</td>
  </tr>
</table>
</body>
</html>";

$dompdf->load_html($html);
$dompdf->render();
$dompdf->set_base_path('localhost/exampls/style.css');
$dompdf->stream("hello.pdf");
?>

Think of dompdf as a web browser that renders to PDF instead of a screen. You feed it a valid HTML document just like you would any web browser.

将 dompdf 视为呈现为 PDF 而不是屏幕的 Web 浏览器。您可以像处理任何 Web 浏览器一样向其提供有效的 HTML 文档。

回答by internals-in

dompdf supports the some CSS 2.1 selector syntaxes. dompdf also supports elements with multiple class attributes (e.g. <p class="red bold">foo</p> matches p.red and p.bold).

dompdf 支持一些 CSS 2.1 选择器语法。dompdf 还支持具有多个类属性 ( e.g. <p class="red bold">foo</p> matches p.red and p.bold) 的元素。

Note that the CSS selectors are case sensitive in dompdf (e.g. .foowill not match <div class="FOO">bar</div>)

请注意,CSS 选择器在 dompdf 中区分大小写(例如.foo不会匹配<div class="FOO">bar</div>

Read http://code.google.com/p/dompdf/wiki/CSSCompatibilityor Git https://github.com/dompdf/dompdf/wiki/CSSCompatibility

阅读http://code.google.com/p/dompdf/wiki/CSSCompatibility或 Git https://github.com/dompdf/dompdf/wiki/CSSCompatibility

Refer DOMPDF doesn't work with external css file

参考DOMPDF 不适用于外部 css 文件

CSS not working with DOMPDF

CSS 不适用于 DOMPDF