MPDF 7.x无法设置Arial字体,是配置错误还是程序Bug?
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?
Let's break down the possible issues and fixes step by step:
Verify font file existence and path accuracy
First, confirm thatarial.ttfactually exists in the path__DIR__ . '/../../Resources/Public/Fonts'. You can quickly test this withvar_dump(file_exists(__DIR__ . '/../../Resources/Public/Fonts/arial.ttf'))—if the result isfalse, 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 samearial.ttffile 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 (usuallyariali.ttf), replace theIvalue 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 thetmp/mpdfdirectory (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 thefont-family: Arial, sans-serifstyle 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 callingWriteHTML()). 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

