DOMPDF 不适用于外部 css 文件 [英] DOMPDF doesn't work with external css file

查看:19
本文介绍了DOMPDF 不适用于外部 css 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码.

  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.对应视图的代码(index/dom.phtml)

2.Code of corresponding view (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.还有我的css文件:

3.And my css file:

div {color: red;}

如何让它发挥作用?

更新:

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

To make it works I changed the following things:

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

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

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

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

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

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

推荐答案

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

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.

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

另请参阅 DomPDF 的手册以了解此功能.

See also the manual of DomPDF for this feature.

这篇关于DOMPDF 不适用于外部 css 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆