用 Dompdf 指定字体?

Specify font with Dompdf?

我上传了一个小例子here。它由3个文件组成。

我想使用 DOMPDF 在服务器端呈现 Laravel 视图。我意识到我使用的字体 Open Sans 不工作,所以我做了一个更简单的测试,它显示了同样的问题。

如果您尝试调用 index.php,您将获得此 PDF,其中 Lorem 标题未使用 Open Sans 呈现。

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>

Dompdf(直至并包括 0.8.3)不支持数字字体粗细 (see issue #675)。您必须修改样式表以仅使用 "bold" 或 "normal" 作为字体粗细。