Laravel DomPdf Add Custom Font

Laravel DomPdf Add Custom Font

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel DomPdf Add Custom Font

Integrating custom typography into PDFs generated by Dompdf within a Laravel application is often more friction-heavy than it appears in documentation. The core issue usually stems from a misunderstanding of how Dompdf handles font registration versus how the browser renders CSS @font-face. Simply dropping a font file into your public directory and referencing it in a Blade template does not automatically make it available to the PDF renderer.

The confusion often arises when developers look at configuration files like dompdf_font_family_cache.dist.php found within the vendor directory. While this file exists, it is essentially a cache or a placeholder for specific font mappings. Modifying files inside the vendor folder in a Laravel application is generally an anti-pattern because those changes will be overwritten during dependency updates. Furthermore, Dompdf does not automatically scan your project for new font files upon instantiation; it relies on a predefined list of available fonts or explicit registration.

To successfully add an "OLD English" font (or any other custom typeface), you must explicitly tell the Dompdf renderer where to find the font file and what family name to associate with it. This involves three distinct steps: placing the files correctly, configuring the Dompdf instance, and ensuring the CSS references match the registered configuration.

First, prepare your font assets. You need both a TrueType (.ttf) or OpenType (.otf) file for the actual glyphs and a corresponding .woff or .woff2 file if you intend to use Web Fonts, though Dompdf primarily relies on raw TTF/OTF files found in its font directory. Copy your custom font file, let's call it OldEnglish.ttf, into the standard Dompdf font path. The default location is typically vendor/dompdf/fonts/truetype/myfont. If you are using a custom configuration or have altered the vendor structure, ensure the path matches exactly where Dompdf expects to look during initialization.

Next, you must register the font family. This is where the dompdf_config.php file comes into play, which should reside in your project's root or be loaded via a service provider. You need to define the font family name and map it to the specific file path. Here is how you structure that configuration:

// config/dompdf.php
return [
    'font_dir' => __DIR__ . '/../vendor/dompdf/fonts',
    'log_output_type' => 'error_log',
    'default_font_size' => 10,
    'is_html5_parser_enabled' => true,
    'is_css_allowed' => true,
    'is_remote_enabled' => false,
    'temp_dir' => sys_get_temp_dir(),
    'font_cache' => __DIR__ . '/../storage/app/public/fonts',

    // Register custom fonts here
    'fonts' => [
        'OldEnglish' => [
            'normal' => __DIR__ . '/vendor/dompdf/fonts/truetype/oldenglish.ttf',
            'bold'   => null, // Set if you have a bold variant
        ],
    ],
];

Notice the fonts array. The key 'OldEnglish' is the family name you will reference in your CSS. The value maps the style (normal, bold, italic) to the absolute path of the font file. If you do not have a bold variant, setting it to null tells Dompdf to fallback to the normal weight or simply use the defined file for all weights.

Finally, update your Blade view or HTML string passed to the PDF generator. You must use the exact family name registered in the configuration.

<style>
    .document-title {
        font-family: 'OldEnglish', serif;
        font-size: 24pt;
        margin-bottom: 10px;
    }
</style>

<h1 class="document-title">The Grand Opening</h1>
<p>This document utilizes the custom Old English typeface.</p>

A common pitfall is assuming that adding the font file alone resolves the issue. Dompdf does not hot-reload its font cache automatically when files change on disk between requests unless you clear the cache or restart the application. Additionally, if you are using a service provider to load configuration, ensure the dompdf_config.php is actually being loaded before the PDF generation process runs.

If you encounter issues where the font appears as a fallback (like Arial) despite correct configuration, verify that the file paths are absolute and accessible to the web server user running Laravel. Also, remember that Dompdf parses CSS differently than Chrome; it does not support complex @font-face stacks in the same way. You must declare the font directly in the style block or via inline styles within the HTML string being converted.

For more advanced scenarios involving dynamic font loading or complex layout requirements, checking community resources like LaravelCompany can provide insights into specific package integrations that bridge Dompdf with modern asset management strategies. However, for standard custom fonts, direct configuration remains the most reliable approach.

Our network