Laravel DomPdf Add Custom Font
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Effortlessly Adding Custom Fonts to Laravel DomPDF Generations
Introduction: DomPDF is an open-source PHP library for generating PDF documents from HTML content. With its extensive support for CSS, it’s a powerful tool in many web development projects. One crucial yet challenging aspect of using DomPDF can be integrating custom fonts within your generated PDF files. In this comprehensive blog post, we'll explore the process of including custom fonts, the possible challenges you might encounter, and provide solutions to troubleshoot common issues like using Old English fonts with Laravel.
Body:
1. Including Custom Fonts in View Files
Firstly, ensure your custom font files are properly referenced in your application's view. For instance, assuming your font file is saved as "OldEnglishFont.ttf" in the "fonts" directory inside your Laravel project, you could include it like this:
{{ HTML::style('css/styles.css') }}
Inside styles.css, add the following line:
.custom_font {
font-family: 'OldEnglishFont';
}
Then, in your Laravel view file use it as necessary:
<div class="custom_font">Your Content Here</div>
2. DomPDF Font Family Cache Configuration
DomPDF uses a font family cache to ensure that the library doesn't load each font file every time you generate a new PDF document. This can speed up your application's performance, but for a custom font like Old English, you may need to manually configure the cache. Follow these steps:
1. Backup the existing dompdf_font_family_cache.dist.php file:
cp dompdf/vendor/dompdf/dompdf/config/dompdf_font_family_cache.dist.php dompdf/vendor/dompdf/dompdf/config/dompdf_font_family_cache.original.php
2. Make a copy of the original file:
cp dompdf/vendor/dompdf/dompdf/config/dompdf_font_family_cache.original.php dompdf/vendor/dompdf/dompdf/config/dompdf_font_family_cache.php
3. Open the new file, dompdf_font_family_cache.php, and add your custom font details:
.custom_font {
base_name: OldEnglishFont;
dir: /path/to/your/fonts/OldEnglishFont.ttf;
}
4. Save the updated file and replace the original one by moving dompdf_font_family_cache.php into the dist folder:
mv dompdf/vendor/dompdf/dompdf/config/dompdf_font_family_cache.php dompdf/vendor/dompdf/dompdf/config/dompdf_font_family_cache.dist.php
5. Run the following command to clear DomPDF's cache:
composer update
3. Common Issues and Solutions
Ensure that your custom font file is in the correct format. Font files like TrueType (e.g., .ttf) or Open Type (e.g., .otf) are commonly compatible with HTML rendering engines. If using a different type of font, you may need to convert it to one of these formats for Laravel DomPDF to recognize it correctly.
In case your custom font doesn't render properly in the generated PDF file, you can try adding the @font-face rule directly into your CSS file:
@font-face {
font-family: 'OldEnglishFont';
src: url('font/OldEnglishFont.ttf') format('truetype');
}
Then, in your view, reference the custom class you created containing the @font-face rule instead of simply specifying ".custom_font":
<div class="class_with_font">Your Content Here</div>
Conclusion:
In conclusion, adding a custom font to your Laravel application's DomPDF generated PDFs can be done by carefully referencing the font file in your view, configuring the font family cache, and troubleshooting any potential issues that may arise. By following these best practices, you can ensure that your PDF documents are visually appealing and maintain consistency with your branding while staying within your application's capabilities.