Search code examples
phphtmlcssfontsdompdf

Fonts work in HTML but not rendered PDF Dompdf v0.8.0


I realize there is a whole host of somewhat related topics, but I just can't seem to find one that's the same and / or includes a solution that solves my problem.

I'm sending an HTML document to dompdf which includes embeded CSS and includes font-face declarations and class definitions. The fonts are on the same server and I've tried with both relative and absolute paths and can't seem to get them to work.

I'm including the following in the head of the HTML document I then send to dompdf.

<style type="text/css">
    @font-face {
        font-family: "Roboto";
        font-weight: normal;
        font-style: normal;
         src: url( "http://localhost/resources/fonts/Roboto/Roboto-Regular.ttf") format('truetype');
     }
      .font_alpha { font-family: "Roboto";}`

    /* .... Other css rules here ... */

</style>

When I echo the html rather than send it on to dompdf I can see that the font for any element with the .font_alpha class applied is correct, in inspector it also shows that it's using the network resource yet when I call the following on the html, the font doesn't get applied.

//This is where I would display the html directly for testing
//echo $report_html; exit;

$dompdf = new DOMPDF();
$dompdf -> load_html( $report_html );
$dompdf -> set_paper( $this -> paper_size, $this -> paper_orientation );
$dompdf -> render();
$dompdf -> output();

The pdf(s) get generated correctly with all css formating from the rest of the rules being correctly applied except for the fonts.

To be honest, I'm not sure if I need to be installing these fonts in an additional way like using cmd line load_font.php thirdparty lib or not. Based on what I believe I can understand about version 0.8.0, I shouldn't need to, correct?

Any advice or input?


Solution

  • Dompdf's CSS parsing logic can be a bit finicky. The space in front of the URL string is tripping Dompdf up. If you modify your CSS to read src: url("http...") format('truetype'); it should work as expected.