简体   繁体   中英

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

I just can't get it to work. No errors, but the font is still the default mPDF font. 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?

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

adding font to 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.

Heres my php file:

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:

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.

Here's the real working solution as instructed in the docs : "Define the font details in fontdata configuration variable - font name must be lowercase".

So BentonSans must be changed to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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