DOMPDF doesn't work with external css file

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.


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

  • https://github.com/dompdf/dompdf/search?q=set_protocol
  • https://groups.google.com/forum/?_escaped_fragment_=topic/dompdf/uBWdQbug_dM
  • http://code.google.com/p/dompdf/wiki/CSSCompatibility

Hope this saves someone else few hours of time.