You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MPDF 7.x无法设置Arial字体,是配置错误还是程序Bug?

Issue: MPDF 7.x still uses DejaVuSansCondensed despite configuring Arial font

I've configured MPDF 7.x following official guidelines, added Arial font to the configuration and set it as the default font, also added inline CSS font-family: Arial, sans-serif in the template. However, when I download the generated PDF, it still uses the default DejaVuSansCondensed font instead of Arial.

Here's my configuration code:

$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdf = new \Mpdf\Mpdf([
    'fontDir' => array_merge($fontDirs, [__DIR__ . '/../../Resources/Public/Fonts',]),
    'fontdata' => $fontData + [
        'Arial' => [
            'R' => 'arial.ttf',
            'I' => 'arial.ttf',
        ]
    ],
    'default_font' => 'Arial'
]);
$mpdf->WriteHTML($pdfTemplate->render());
$mpdf->Output(__DIR__.'/document.pdf','F');

Is this a configuration mistake or a program bug?


Answer

Let's break down the possible issues and fixes step by step:

  • Verify font file existence and path accuracy
    First, confirm that arial.ttf actually exists in the path __DIR__ . '/../../Resources/Public/Fonts'. You can quickly test this with var_dump(file_exists(__DIR__ . '/../../Resources/Public/Fonts/arial.ttf'))—if the result is false, MPDF will automatically fall back to the default DejaVuSansCondensed font. Double-check for typos in the filename or directory structure too.

  • Fix the font style mapping in fontdata
    Your current config uses the same arial.ttf file for both regular (R) and italic (I) styles. While this won't break the setup entirely, it's not ideal. If you have the italic variant (usually ariali.ttf), replace the I value with that file. Even if you don't, ensure the style keys (R, I, etc.) are correctly named as MPDF relies on these identifiers to apply styles.

  • Clear MPDF's font cache
    MPDF caches font metadata to boost performance. If you just added the Arial font, the old cache might still be prioritizing DejaVuSansCondensed. Locate the tmp/mpdf directory (this is usually in your project's temp folder or system temp directory) and delete all font-related cache files. This forces MPDF to re-scan and load your new font configuration.

  • Validate your template's inline CSS
    Make sure the rendered HTML from $pdfTemplate->render() actually includes the font-family: Arial, sans-serif style on the intended elements. Sometimes template engines might strip styles, or more specific CSS rules could be overriding your inline font setting. Debug this by printing the output of $pdfTemplate->render() to check the final HTML.

  • Explicitly set the default font before writing HTML
    Add $mpdf->SetFont('Arial'); right after initializing the MPDF instance (before calling WriteHTML()). This enforces the default font at the MPDF level, bypassing any potential CSS parsing issues in your template.

  • Check for character support gaps
    If your template contains characters that Arial doesn't support (e.g., special symbols or non-Latin characters), MPDF will fall back to the default font to render those characters. Ensure Arial covers all the characters in your content, or add a fallback font in your CSS that handles missing characters.

In most cases, the issue boils down to an incorrect font path, missing font file, or outdated cache. Start with verifying the file path and clearing the cache—these are the most common culprits.

内容的提问来源于stack exchange,提问作者Naberd Zendi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:48:40