13

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.

Here is my code.

  1. Code of controller's action, which generate pdf

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)

<!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:

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

$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.

<link type="text/css" href="pdf.css" rel="stylesheet" />
Tamara
  • 2,910
  • 6
  • 44
  • 73
  • In case someone finds this on a search engine (just like I did), please note there's another option called `DOMPDF::set_protocol()`, see my answer below for more info. – ᴍᴇʜᴏᴠ Oct 19 '14 at 21:50

3 Answers3

13

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

See also the manual of DomPDF for this feature.

FullStackFool
  • 1,071
  • 9
  • 15
Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99
6

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

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(WWW_ROOT);
$dompdf->set_base_path('/');

WWW_ROOT here is a CakePHP constant pointing to the webroot folder of my application. Note that it has a trailing slash.

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.

Hope this saves someone else few hours of time.

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • Thanks @aexl, this saved me a lot of time – DatsunBing May 29 '19 at 22:53
  • Thanks, but what worked for me was ```$options->setIsRemoteEnabled(true); $options->setChroot(array_merge($options->getChroot(), [WWW_ROOT]));```. Actually, after setting the ```Chroot``` I didn't even have to ```$dompdf->setBasePath(WWW_ROOT);``` which was as expected after reading the docs. I think it wouldn't work without CHROOT for security. Check the [docs](https://github.com/dompdf/dompdf/wiki/Usage#security-restrictions-for-local-files). – Fr0zenFyr Apr 09 '21 at 09:27
0

In addition to @Jurian Sluiman's answer, I had to allow Dompdf to access the base path in order for CSS styling and images to work:

$dompdf = new Dompdf(['chroot' => __DIR__]);
$dompdf->setBasePath(__DIR__ . '/path/to/assets/'));
$dompdf->loadHtml($html);
$dompdf->render();
Midorina
  • 11
  • 1
  • 5