Search code examples
phpcssfontsdompdf

Specify font with Dompdf?


I have uploaded a small example here. It is composed of 3 files.

I would like to use DOMPDF to render a Laravel view on the server side. I realized the font I am using Open Sans is not working so I made a simpler test which shows the same issue.

If you try to call index.php wou will get this PDF in which the Lorem title is not rendered with Open Sans.

enter image description here

composer.json

{
    "name": "nowox/test-dompdf",
    "require": {
        "dompdf/dompdf": "^0.8.3",
        "webfontkit/open-sans": "^1.0"
    }
}

index.php

<?php

require 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('defaultFont', 'Open Sans');
$options->set('fontDir', 'vendor/webfontkit/open-sans/fonts');

$dompdf = new Dompdf($options);
$dompdf->loadHtml(file_get_contents('template.html'));


$dompdf->setPaper('A4');
$dompdf->render();
$dompdf->stream();

index.html

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" type="text/css" href="vendor/webfontkit/open-sans/open-sans.min.css"/>
        <style>
        * {
            font-family: 'Open Sans';
        }
        h1 {
            font-family: 'Open Sans';
        }
        </style>
    </head>
    <body>
        <h1>Lorem</h1>
        <p>Lorem ipsum</p>
    </body>
</html>

Solution

  • Dompdf (up to and including 0.8.3) does not support numeric font weights (see issue #675). You'll have to modify your stylesheet to only use "bold" or "normal" for the font weight.