Lucas
Lucas

Reputation: 452

Dompdf rendering spaces incorrectly with custom font

I'm using Dompdf v2.0.1 to make a report. I'm using a custom font from Google Fonts (Josefin Sans) but the spaces are rendered incorrectly.

enter image description here

But when rendering the page as HTML, spaces are displayed correctly.

enter image description here

Below I'll paste the HTML and PHP code I'm using to generate the PDF.

<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@300;400;700&display=swap" rel="stylesheet">
        <title>Relatório de atendimentos</title>
        <style>
            @page {
                margin: 16px;
                padding: 0;
            }

            body {
                font-family: 'Josefin Sans', sans-serif;
                font-size: .8em;
            }
            table {
                width: 100%;
                border-collapse: collapse;
                border: none;
            }
            td, th {
                padding: .3em;
            }
            td {
                border: solid 1px #000;
                font-weight: 300;
            }
            .title {
                font-size: 1.2em;
                color: #FFF;
                text-align: center;
                background-color: #363435;
                border-color: #363435;
            }
        </style>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th class="title">Relatório detalhado de tickets</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <b>GERADO POR: </b> sadsadsad
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>
<?php

declare(strict_types=1);

namespace App\Actions\Report;

use Dompdf\Dompdf;

class GenerateReport
{
    public static function run(array $data): string
    {
        return GenerateReportString::get($data);

        $pdf = new Dompdf([
            'isRemoteEnabled' => true,
            'isJavascriptEnabled' => false,
            'dpi' => 200,
        ]);
        $pdf->setPaper('A4', 'landscape');
        $pdf->loadHtml(
            GenerateReportString::get($data)
        );
        $pdf->render();
        return $pdf->output();
    }
}

I tried setting the charset to UTF-8 in the HTTP header (Content-Type: application/pdf; charset=UTF-8), but the result is the same.

Upvotes: 0

Views: 723

Answers (1)

BrianS
BrianS

Reputation: 13914

This is due to a font subsetting bug in the underlying php-font-lib library, see issue 2445. Not all fonts are impacted, but for those that are you can temporarily work around the issue by disabling subsetting.

$dompdf = new Dompdf(["IsFontSubsettingEnabled" => false]);

Note: This isn't a great solution if the font is large since it will impact the overall PDF size.

Upvotes: 1

Related Questions