Search code examples
phpcssmpdfgoogle-fonts

MPDF 8+ google font doesen't register


I want to use a google font with MPDF. I followed their instruction, step by step, but it doesn't work for me. I register the MPDF object as following:

$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];


$mpdf = new \Mpdf\Mpdf([
'fontDir' => array_merge($fontDirs, [
    __DIR__ . '/font',
]),
'fontdata' => $fontData + [
    'comfortaa' => [
        'R' => 'Comfortaa-Regular.ttf',
        'B' => 'Comfortaa-Bold.ttf',
    ]
],
]);

$mpdf->WriteHTML($html);

From google, I use the font Comfortaa. I downloaded it as a static TTF file, which google gives you the ability to. I used 400 version for Comfortaa-Regular.ttf and the 700 for Comfortaa-Bold.ttf. In the current folder, there is a folder called fonts.

I'm declaring the CSS as inline style like this:

<td class="content-block powered-by" style="font-family: comfortaa; vertical-align: top; font-size: 13px; color: #b9b9b9; text-align: center;">
    Powered by <a href="https://example.com" style="font-size: 13px; text-align: center; color: #b9b9b9;"><strong style="text-decoration:none;"><span>Flowmarket</span></strong></a>.
</td>

But still, the font doesn't load. I get no error message too, so I don't know where to start. Is it possible that the Google font has a wrong encoding? I'm using MPDF Version 8.0.5.0.


Solution

  • Okay, this is kinda embarrassed I guess. I was previewing the file on its location URL in chrome and you need to reset the browser cache in order to see the changes...