Melvin
Melvin

Reputation: 154

How to properly embed a Google Font in DomPDF? Breaks when printed

I try to generate a PDF using DomPDF (v1.0.2) with Open Sans as a font.

I load Open Sans directly from Google Fonts (which should work according to DomPDF docs) and the resulting PDF seems to be fine.

The issue occurs when printing, apparently the fonts are not properly embedded which breaks the font when printed.

Simplified code example:

$pdf = new Dompdf(new Options([
    'defaultPaperSize'          => 'a4',
    'defaultPaperOrientation'   => 'portrait',
    'isRemoteEnabled'           => true,
    'allowUrlFopen'             => true,
    'isHtml5ParserEnabled'      => true
]));

$pdf->loadHtml('
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;1,400;1,600&display=swap" rel="stylesheet">
    <style>
        span {
            font-family: "Open Sans", sans-serif;
        }
    </style>
    <body>
        <span>This should be in Open Sans</span>
    </body>
');
$pdf->render();
$pdf->stream();

On screen:

enter image description here

Printed:

enter image description here

Upvotes: 2

Views: 1520

Answers (1)

Melvin
Melvin

Reputation: 154

Figured it out, the issue was DomPDF not detecting the used font-styles properly, which caused it to create an incomplete sub-set.

Adding this to the options solved the issue:

'isFontSubsettingEnabled' => false

Upvotes: 1

Related Questions