简体   繁体   English

无法加载 mPDF 中的自定义字体

[英]Custom font in mPDF won´t load

I'm using version 7.x of mPDF and tried to follow this documentation: https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html我正在使用 mPDF 7.x 版并尝试遵循此文档: https ://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html

I just can't get it to work.我只是无法让它工作。 No errors, but the font is still the default mPDF font.没有错误,但字体仍然是默认的 mPDF 字体。 I also tried to do it another way with the answers from these:我还尝试使用以下答案以另一种方式做到这一点:

How to generate PDF using mPDF and add custom Google font to it? 如何使用 mPDF 生成 PDF 并向其添加自定义 Google 字体?

php mPDF, impossible to set font-family and font-size php mPDF,无法设置字体系列和字体大小

adding font to mPDF 将字体添加到 mPDF

But I guess they don´t work, as they might only be for older version than 7.X ...So here's is my latest attempt trying to use the information for the 7.x documentation.但我想它们不起作用,因为它们可能只适用于比 7.X 旧的版本......所以这是我尝试使用 7.x 文档的信息的最新尝试。

Heres my php file:这是我的 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');

And my css:还有我的 css:

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

My custom fonts are stored in "fonts" which is in the same folder as the php file.我的自定义字体存储在与 php 文件位于同一文件夹中的“字体”中。

Here's the real working solution as instructed in the docs : "Define the font details in fontdata configuration variable - font name must be lowercase".这是 文档中指示的实际工作解决方案:“在 fontdata 配置变量中定义字体详细信息 - 字体名称必须为小写”。

So BentonSans must be changed to bentonsans .所以BentonSans必须改变,以bentonsans

Code would be:代码将是:

$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'
    ]
);

The issue is in Mpdf version 7 the configuration is passed as a single parameter (an array), while you were passing in multiple parameters to the constructor.问题是在 Mpdf 版本 7 中,配置作为单个参数(数组)传递,而您将多个参数传递给构造函数。

This is a valid configuration:这是一个有效的配置:

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

Got it to work.得到它的工作。 Not sure what did the trick, but here's the working code:不知道有什么技巧,但这是工作代码:

<?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');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM