Dompdf and set different font-family

Dompdf and set different font-family

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

Stefan Bogdanescu

Founder & Senior Architect

Dompdf and set different font-family

Dompdf ignores your font-family attribute applied to your CSS because it operates on a completely different rendering model than modern browsers. When you generate a PDF using the standard approach shown below, Dompdf defaults to Times New Roman if the requested font isn't explicitly defined in its internal configuration or if the file path is unresolved.

$pdf = PDF::loadHTML($view);
return $pdf->stream();

In this snippet, the $view variable likely contains HTML with inline styles or a <style> block specifying NeutraText-Book. Dompdf receives this as an internal HTML representation (often using the DOMDocument library) and attempts to map CSS properties to its own limited font registry. It does not automatically download web fonts from CDNs like Google Fonts, nor does it search your local public/fonts directory unless you explicitly configure it to do so. Consequently, when it encounters NeutraText-Book, it cannot resolve the file path and falls back to the system default: Times New Roman.

To render a custom font in Dompdf, you must bridge the gap between the web font format (usually .ttf or .otf) and Dompdf's internal font registry. This involves three critical steps: preparing the font files, registering them with the library, and updating your CSS to reference the correct family name.

Preparing the Font Files

Dompdf requires TrueType (ttf) or OpenType (otf) fonts located in a directory accessible by the PHP process. If you are using a framework like Laravel, the standard practice is to place these files in public/fonts or a dedicated directory like resources/fonts. However, simply dropping a file there isn't enough; Dompdf must know where to look for it.

You can configure the font path globally in your application's entry point (e.g., bootstrap/app.php in Laravel) by extending the configuration:

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpFontCached', false); // Optional, depends on your setup
$dompdf = new Dompdf($options);

// Set the font path explicitly if not done via config array
$dompdf->loadFontConfig();

Registering Custom Fonts

The core mechanism for overriding the default font is the font_config array. You need to map a CSS-friendly name (like NeutraText) to the actual file path of your .ttf file. This configuration must be passed to Dompdf before rendering the HTML.

Here is how you define a custom font mapping:

$fontPath = public_path('fonts'); // Adjust based on your project structure

// Define the font families and their paths
$fontData = [
    'NeutraText' => [
        'normal' => $fontPath . '/NeutraText-Book.ttf',
        'bold'   => $fontPath . '/NeutraText-Bold.ttf',
        // You can add italic, etc., if needed
    ],
    'Verdana' => [
        'normal' => $fontPath . '/Verdana.ttf',
        'bold'   => $fontPath . '/Verdana-Bold.ttf',
    ],
];

$dompdf->loadFontConfig($fontData);

Once this mapping exists, Dompdf will replace any CSS rule requesting NeutraText-Book (or just NeutraText) with the actual font file from your path. Note that you should use a consistent naming convention; if your CSS says font-family: NeutraText-Book, ensure your config key matches exactly or adjust the CSS to match the config key (usually the base name is preferred).

Updating Your CSS

After registering the fonts, update your view's CSS to reference the registered family name. If you registered NeutraText as a family with multiple weights, your CSS should look like this:

.sub-header {
    font-size: 1.4rem;
    text-transform: uppercase;
    font-family: NeutraText; /* Matches the key in fontData */
    padding-bottom: 1.5rem;
}

If you need specific weights that aren't registered, Dompdf will fall back to the next available weight or the default again. Always ensure that normal, bold, and any italic variants are explicitly mapped if your HTML uses <strong> tags or CSS weight modifiers.

Troubleshooting Common Pitfalls

A frequent issue developers encounter is forgetting to call $dompdf->loadFontConfig(). Without this call, Dompdf does not scan the additional directories for custom fonts. Another common mistake is assuming that web fonts loaded via @font-face in a <style> block will work automatically. They won't. Dompdf parses the CSS but cannot fetch external URLs; it strictly relies on the local file paths you provide in the configuration.

Finally, remember that font rendering can vary slightly between Dompdf and browser previews because of how each engine handles anti-aliasing and fallback chains. Always preview your generated PDF output to verify that the custom fonts render as expected before committing the code.

Our network