DOMPDF doesn't work with external css file

后端 未结 2 872
说谎
说谎 2020-12-07 01:03

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

相关标签:
2条回答
  • 2020-12-07 01:26

    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.

    0 讨论(0)
  • 2020-12-07 01:28

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

    0 讨论(0)
提交回复
热议问题