DOMPDF 不适用于外部 css 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17185531/
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-29 23:16:15  来源:igfitidea点击:

DOMPDF doesn't work with external css file

csszend-frameworkdompdf

提问by Tamara

I'm using Zend Framework and DOMPDF library. When I test it with inline css everything works perfectly. But when I tried to move css code to the external file rules are not applied to the html page.

我正在使用 Zend 框架和 DOMPDF 库。当我用内联 css 测试它时,一切正常。但是当我尝试将 css 代码移动到外部文件时,规则不适用于 html 页面。

Here is my code.

这是我的代码。

  1. Code of controller's action, which generate pdf
  1. 控制器动作代码,生成pdf

require_once("DomPdf/dompdf_config.inc.php");

require_once("DomPdf/dompdf_config.inc.php");

    $this->_helper->layout->disableLayout();

    $html = $this->view->render('index/dom.phtml');

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

    $pdfContent =   $dompdf->output();

    file_put_contents('sample.pdf', $pdfContent);

    die("test");

2.Code of corresponding view (index/dom.phtml)

2.对应视图的代码(index/dom.phtml)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link type="text/css" href="/themes/css/pdf.css" rel="stylesheet"   media="screen"/>

</head>
<body>
    <div>Tamara testing</div>
    <table border="1">
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
        <tr>
            <td>Value 1</td>
            <td>Value 2</td>
        </tr>
    </table>
</body>

</html>

3.And my css file:

3.和我的css文件:

div {color: red;}

How to make it works?

如何使它起作用?

UPDATE:

更新:

To make it works I changed the following things:

为了使其正常工作,我更改了以下内容:

1.In controller's action add base path for external files

1.在控制器的动作中添加外部文件的基本路径

$dompdf->set_base_path(APPLICATION_PATH."/../public/themes/css/");

2.In view change href attribute of the link tag. Make it relative to the base path set in step 1.

2.在视图中修改link标签的href属性。使其相对于步骤 1 中设置的基本路径。

<link type="text/css" href="pdf.css" rel="stylesheet" />

采纳答案by Jurian Sluiman

This has in fact nothing to do with Zend Framework, but you need to supply DomPDF the right path to load the "external" files from.

这实际上与 Zend Framework 无关,但您需要为 DomPDF 提供正确的路径以从中加载“外部”文件。

$dompdf = new DOMPDF();
$dompdf->setBasePath(realpath(APPLICATION_PATH . '/path/to/css/'));
$dompdf->loadHtml($html);
$dompdf->render();

See also the manualof DomPDF for this feature.

有关此功能,另请参阅DomPDF的手册

回答by aexl

@Jurian Sluiman is on the right track, though his answer did not help me, unfortunately.

@Jurian Sluiman 走在正确的轨道上,但不幸的是,他的回答对我没有帮助。

I had to spend some time in order to find the solution that worked for me, which was using DOMPDF::set_protocol():

我不得不花一些时间才能找到对我有用的解决方案,该解决方案使用DOMPDF::set_protocol()

$dompdf->set_protocol(WWW_ROOT);
$dompdf->set_base_path('/');

WWW_ROOThere is a CakePHP constantpointing to the webroot folder of my application. Note that it has a trailing slash.

WWW_ROOT这是一个指向我的应用程序的 webroot 文件夹的CakePHP 常量请注意,它有一个尾部斜杠。

The best part is that this seems like improper usage of set_protocol(). But I'm fine with that as long as it makes the CSS work.

最好的部分是这似乎是set_protocol(). 但只要它使 CSS 工作,我就可以接受。

Hope this saves someone else few hours of time.

希望这可以为其他人节省几个小时的时间。