Dompdf offers a convenient way to create PDFs, but users often encounter discrepancies where the specified font-family in CSS is overlooked, resulting in the default Times New Roman font in the PDF output. This commonly occurs when a custom font is employed, leading to the question: how can you set and enforce the desired font?
The solution lies in understanding how Dompdf manages fonts. PDF documents inherently support a limited set of fonts. To incorporate other fonts, Dompdf requires them to be pre-loaded or referenced in CSS using the @font-face rule. This pre-loading is essential for generating accurate font metrics during typesetting.
Dompdf supports Type 1 and TrueType fonts, provided their font metrics are available. The PHP-based php-font-lib library assists in loading and sub-setting these fonts.
Methods for Font Loading
There are several approaches to load fonts:
Example: CSS @font-face Rule
To load a font using a CSS @font-face rule, incorporate the following code:
@font-face { font-family: 'Open Sans'; font-style: normal; font-weight: normal; src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format('truetype'); }
Note: The load_font.php and admin site methods will be deprecated in dompdf 0.7.0 and later.
The above is the detailed content of How to Set and Enforce the Desired Font in Dompdf PDF Generation?. For more information, please follow other related articles on the PHP Chinese website!