dongtan5555
2018-10-08 19:38
浏览 218
已采纳

mPDF中的自定义字体不会加载

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpd7195 2018-10-22 23:10
    已采纳

    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',
        ]
    );
    
    点赞 打赏 评论
  • doumaogui5937 2018-10-09 18:23

    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');
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题