Search code examples
phppdf-generationdompdf

Custom Fonts in DomPDF


I'm Having a problem getting custom fonts to work in DOMPDF. I am using Drupal 7 as my backend, but I don't think that is particularly relevant to the issue I am having. When I return straight HTML, my custom fonts work:

<?php
$HTML = <<<HTML

<!DOCTYPE html>
<html>
<head>

<style>

@font-face {
font-family: 'PressStart2PRegular';
    src: url('http://fontlibrary.org/assets/fonts/press-start-2p/6b0a4bbcec8eb53940cbfcb409a788ee/74496d9086d97aaeeafb3085e9957668/PressStart2PRegular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
  }

</style>
</head>

<body>
<p><span class="fa fa-envelope"></span> envelope</p>
<span style="font-family: 'PressStart2PRegular'">THwabubu Thwabubu thwabubu</span>
<p style="font-family: PressStart2PRegular;">dsafda</p>
</body>
</html>

HTML;

echo $HTML;
?>

However, when I return the same thing with the DOMPDF library, my custom font doesnt apply:

<?php
$HTML = <<<HTML

<!DOCTYPE html>
<html>
<head>

<style>

@font-face {
font-family: 'PressStart2PRegular';
src: url('http://fontlibrary.org/assets/fonts/press-start-2p/6b0a4bbcec8eb53940cbfcb409a788ee/74496d9086d97aaeeafb3085e9957668/PressStart2PRegular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}

</style>
</head>

<body>
<p><span class="fa fa-envelope"></span> envelope</p>
<span style="font-family: 'PressStart2PRegular'">THwabubu Thwabubu thwabubu</span>
<p style="font-family: PressStart2PRegular;">dsafda</p>
</body>
</html>

HTML;

//DomPDF Stuffs
require_once "sites/all/libraries/dompdf/autoload.inc.php";
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($HTML);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
?>

According to what I've read on the DOMPDF documentation, and this answer: Dompdf and set different font-family I feel like I am operating withing the acceptable parameters for a Custom font in DOMPDF, but I clearly, something is wrong. I'm using DOMPDF version 0.8.0. I know this is awfully similar to other questions... but I can't figure out why this instance isn't working.


Solution

  • This ended up being a permissions issue. Once I changed the permissions on the DomPDF library folder to make it writeable, my fonts started working as expected.