0

I'm generating a document using barryvdh/laravel-dompdf. I want to use the Nunito font in my document.

My controller method:

public function generateSocialBanner(EditionUser $editionUser) {
    $domPDF = $this->documentsManager->generateDocument($editionUser);
    return $domPDF->stream();
}

The generateDocument() method:

// use Barryvdh\DomPDF\Facade\Pdf as DomPDFFacade;

private function generateDocument() {
    $myContent = '<h1>Some text</h1>';
    $document = DomPDFFacade::loadView('documents.banner', compact('myContent'));

    // this can help with font not working properly but makes the document size a tad bigger
    $document->setOption('isFontSubsettingEnabled', false);
    // setup font configuration to allow DomPdf to compile Nunito
    $tmpDir = sys_get_temp_dir();
    $document->setOption('fontDir', $tmpDir);
    $document->setOption('fontCache', $tmpDir);
    $document->setOption('tempDir', $tmpDir);

    $document->setPaper('L');
    $document->output();

    return $document;
}

The documents/banner.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>BANNER</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        @font-face {
            font-family: 'Nunito';
            src: url("{{ Storage::disk('public')->path('fonts/nunito/static/Nunito-Regular.ttf') }}") format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        @font-face {
            font-family: 'Nunito';
            src: url("{{ Storage::disk('public')->path('fonts/nunito/static/Nunito-Bold.ttf') }}") format('truetype');
            font-weight: bold;
            font-style: normal;
        }

        @font-face {
            font-family: 'Nunito';
            src: url("{{ Storage::disk('public')->path('fonts/nunito/static/Nunito-Italic.ttf') }}") format('truetype');
            font-weight: normal;
            font-style: italic;
        }

        @font-face {
            font-family: 'Nunito';
            src: url("{{ Storage::disk('public')->path('fonts/nunito/static/Nunito-BoldItalic.ttf') }}") format('truetype');
            font-weight: bold;
            font-style: italic;
        }

        html {
            margin: 0;
            padding: 0;
        }

        body {
            font-size: 13px;
            font-family: 'Nunito', sans-serif;
        }
    </style>
</head>
<body>
<div>
    {!! $myContent !!}
</div>
</body>
</html>

Instead of having Some text correctly rendered in Nunito font, it is replaced with strange characters:

the text is made of characters I don't know

The font is correctly loaded, as it does not fallback on the default sans-serif setting.

When I copy-paste the text, I get Some text in my clipboard.

System information:

  • PHP 7.4
  • Laravel 8
  • barryvdh/laravel-dompdf 2.1.1
  • dompdf/dompdf 2.0.3

Why do I have this problem?

Update: using only DomPDF, without the barryvdh/laravel-dompdf bridge, is working fine.

1 Answer 1

0

It was because of barryvdh/laravel-dompdf. As soon as I got rid of the bridge, the fonts worked properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.