Home > Article > Backend Development > How to Use Custom Fonts with Dompdf: A Guide to Overcoming Font-Family Configuration Challenges.
Dompdf and Custom Font-Family Configuration
When generating PDFs using Dompdf, users may encounter difficulties overriding the default Times New Roman font with a specified font-family. The root cause lies in Dompdf's internal support for a limited set of predefined fonts.
Understanding Font Support in Dompdf
Dompdf embeds referenced fonts in PDF documents if they are pre-loaded or accessible through CSS @font-face rules. To utilize custom fonts, they must be loaded prior to generating the PDF.
Font Loading Options
Dompdf offers three methods for loading custom fonts:
1. CSS @font-face Rules
Load fonts at runtime without requiring command line access.
@font-face { font-family: 'Open Sans'; font-style: normal; font-weight: normal; src: url(font.ttf) format('truetype'); }
2. Command Line Utility
Use the bundled utility dompdf/load_font.php if command line access is available.
load_font.php "NeutraText-Book" /path/to/neutratext.ttf
3. Admin Site
Browse to dompdf/www/fonts.php in the included admin site (username/password modification may be required).
Note: Loaders for extracting font metrics, including load_font.php and the admin site, will no longer be included starting with Dompdf 0.7.0.
Additional Considerations
Conclusion
By leveraging these font loading techniques, developers can specify and utilize custom fonts in Dompdf-generated PDFs, improving the visual appeal and flexibility of their documents.
The above is the detailed content of How to Use Custom Fonts with Dompdf: A Guide to Overcoming Font-Family Configuration Challenges.. For more information, please follow other related articles on the PHP Chinese website!