自定义网页字体通过 laravel-dompdf 错误呈现

问题描述 投票:0回答:1

我正在使用

barryvdh/laravel-dompdf
生成文档。我想在我的文档中使用 Nunito 字体。

我的控制器方法:

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

generateDocument()
方法:

// 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;
}

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>

它没有以 Nunito 字体正确呈现

Some text
,而是替换为奇怪的字符:

the text is made of characters I don't know

字体已正确加载,因为它不会回退到默认的

sans-serif
设置。

当我复制粘贴文本时,我的剪贴板中会出现

Some text

系统信息:

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

为什么我会遇到这个问题?

更新: 仅使用 DomPDF,不使用 barryvdh/laravel-dompdf 桥,工作正常。

dompdf webfonts laravel-dompdf
1个回答
0
投票

这是因为

barryvdh/laravel-dompdf
。一旦我摆脱了桥,字体就可以正常工作了。

© www.soinside.com 2019 - 2024. All rights reserved.