【问题标题】:Custom font in mPDF won´t loadmPDF 中的自定义字体不会加载
【发布时间】:2019-03-13 12:31:38
【问题描述】:

我正在使用 7.x 版的 mPDF 并尝试遵循此文档: https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html

我就是无法让它工作。没有错误,但字体仍然是默认的 mPDF 字体。 我还尝试用另一种方式来回答这些问题:

How to generate PDF using mPDF and add custom Google font to it?

php mPDF, impossible to set font-family and font-size

adding font to mPDF

但我猜它们不起作用,因为它们可能只适用于比 7.X 更旧的版本......所以这是我尝试使用 7.x 文档信息的最新尝试。

这是我的 php 文件:

require_once __DIR__ . '/vendor/autoload.php';

$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/upload'],
    ['fontdata' => $fontData + [
        'BentonSans' => [
            'R' => 'BentonSans.ttf',
            'I' => 'BentonSans-Bold.ttf',
        ]
    ],
    'default_font' => 'BentonSans'
]);

$url = rawurldecode($_REQUEST['url']);
$html = file_get_contents($url);

$stylesheet = file_get_contents('style.css');

$mpdf->setBasePath($url);
$mpdf->AddFontDirectory('fonts');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output('filename.pdf','I');

还有我的 CSS:

body {
    font-family: 'BentonSans';
    font-size: 14px; 
    font-style: normal; 
    font-variant: normal; 
    font-weight: normal; 
    line-height: 20px;
}

我的自定义字体存储在与 php 文件位于同一文件夹中的“字体”中。

【问题讨论】:

  • 我在想我可能是$mPDFO = new mPDF('utf-8', 'A4', 0, 'font name here', 10, 10, 10, 0, 0, 0, 'L');...但我不知道在我的$mpdf = new \Mpdf\Mpdf(['tem....中放置“字体名称”的位置

标签: php mpdf


【解决方案1】:

这是docs 中指示的真正可行的解决方案: “在 fontdata 配置变量中定义字体详细信息 - 字体名称必须为小写”。

所以BentonSans必须改为bentonsans

代码是:

$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdf = new \Mpdf\Mpdf(
    [
        'tempDir' => __DIR__ . '/upload',
        'fontDir' => array_merge($fontDirs, [
            __DIR__ . '/fonts'
        ]),
        'fontdata' => $fontData + [
            'bentonsans' => [
                'R' => 'BentonSans.ttf',
                'I' => 'BentonSans-Bold.ttf',
            ],
        ],
        'default_font' => 'bentonsans'
    ]
);

【讨论】:

  • 我不敢相信它这么简单。这对我有用,非常感谢。我的设置与您的示例相同,但字体的首字母大写。这应该是 IMO 接受的答案。
【解决方案2】:

问题在于,在 Mpdf 版本 7 中,配置作为单个参数(数组)传递,而您将多个参数传递给构造函数。

这是一个有效的配置:

$mpdf = new \Mpdf\Mpdf(
    [
        'tempDir'      => __DIR__ . '/upload',
        'fontdata'     => $fontData + [
                'bentonsans' => [
                    'R' => 'BentonSans.ttf',
                    'I' => 'BentonSans-Bold.ttf',
                ],
        ],
        'default_font' => 'BentonSans',
    ]
);

【讨论】:

  • 字体名称必须为小写。 BentonSans 必须更改为 bentonsans 或任何小写字母。你的答案肯定行不通。
【解决方案3】:

让它工作。不知道有什么诀窍,但这是工作代码:

<?php
require_once __DIR__ . '/vendor/autoload.php';

if (!defined('_MPDF_TTFONTPATH')) {
    // an absolute path is preferred, trailing slash required:
    define('_MPDF_TTFONTPATH', realpath('fonts/'));
    // example using Laravel's resource_path function:
    // define('_MPDF_TTFONTPATH', resource_path('fonts/'));
}

function add_custom_fonts_to_mpdf($mpdf, $fonts_list) {

    $fontdata = [
        'bentonsans' => [
            'R' => 'BentonSans.ttf',
            'B' => 'BentonSans-Bold.ttf',
        ],
    ];

    foreach ($fontdata as $f => $fs) {
        // add to fontdata array
        $mpdf->fontdata[$f] = $fs;

        // add to available fonts array
        foreach (['R', 'B', 'I', 'BI'] as $style) {
            if (isset($fs[$style]) && $fs[$style]) {
                // warning: no suffix for regular style! hours wasted: 2
                $mpdf->available_unifonts[] = $f . trim($style, 'R');
            }
        }

    }

    $mpdf->default_available_fonts = $mpdf->available_unifonts;
}

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/upload']);

add_custom_fonts_to_mpdf($mpdf);

$url = rawurldecode($_REQUEST['url']);
$html = file_get_contents($url);

$stylesheet = file_get_contents('style.css');

$mpdf->setBasePath($url);
$mpdf->AddFontDirectory('fonts');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output('filename.pdf','I');
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2022-01-09
    • 1970-01-01
    • 2017-07-08
    • 2018-02-11
    • 2016-05-30
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多