Overcoming Font Family Differences in Dompdf
When generating a PDF using Dompdf, users often encounter a discrepancy between the specified font-family in CSS and the font used in the generated document. To address this issue, we delve into the intricacies of font loading and selection in Dompdf.
Dompdf supports a limited set of pre-defined fonts by default. To incorporate custom fonts, users must preload or embed them using CSS @font-face rules. Here are three approaches for loading fonts:
1. Using CSS @font-face Rules:
This allows for runtime font loading. CSS rules can point to online font files, facilitating the inclusion of fonts without command line access.
2. Command Line with load_font.php:
If command line access is available, the load_font.php script can be used to load fonts. It requires the font family name, normal and bold font files, as parameters.
3. Admin Website:
Dompdf includes an admin site with a dedicated fonts section where users can browse and load fonts.
In cases where the desired font is not among the pre-defined options, Dompdf supports external font embedding. Using CSS @font-face rules, users can specify a custom font file hosted online. This method requires the font metrics (.afm/.ufm) to be accessible.
It's important to note that starting with Dompdf version 0.7.0, load_font.php and the admin site will no longer be included by default. For further information on font loading and character encoding in Dompdf, refer to the official wiki and other relevant resources.
The above is the detailed content of How can I ensure consistent font usage in PDFs generated with Dompdf?. For more information, please follow other related articles on the PHP Chinese website!