5

i have a problem with loading custom font in dompdf. I use @font-face with src on ttf file but it fails anyway. In browser dancing script loads normaly and works but in pdf there is some default system font. I have font file in fonts folder. I found some information about load_font.php, but in newest dompdf this file doesn't exist so maybe that was an old way of loading fonts.

--INDEX.PHP


 $dompdf = new Dompdf();
 $dompdf->set_option('isRemoteEnabled', true);
 $dompdf->set_option('isHtml5ParserEnabled', true);
 $dompdf->loadHtml($html);
 $dompdf->setPaper('A4', 'landscape');
 $dompdf->render();
 $pdf_output = $dompdf->output();
 

--VOUCHER-TEMPLATE.HTML

@font-face{
 font-family:'Dancing Script';
 src: url('fonts/DancingScript-Regular.ttf');
 }
h1 {
 color: gold;
 font-size: 3em;
 margin-top: 0;
 margin-bottom: 0.5em;
 font-family: 'Dancing Script';
 }
asked May 18, 2025 at 18:32
2
  • 4
    You're using a relative URL to the font file, relative to what? Remember, you're inside DomPDF, you're not serving a HTML page to a browser over a domain. Try an absolute URL, including the domain. Also, check out the options for fontCache and fontDir, better give them valid values. You must also allow DomPDF to get external content. In short, this is rather difficult, but there are examples out here. Commented May 18, 2025 at 19:19
  • 5
    Okay, you're right the problem was in the path but i just added html <link> with font and it works. Important thing is isRemoteEnabled option. Commented May 18, 2025 at 23:03

1 Answer 1

1

My custom fonts were not applied in the PDF, even when I set all options in dompdf/dompdf initialization, gave all directory permissions, etc.

We use really custom fonts, not Google fonts - which are loaded from S3 with CloudFront.

The solution was to use TTF / TrueType fonts. WOFF and WOFF2 were not applied, even when these files were loadable.

Mark Rotteveel
110k241 gold badges160 silver badges233 bronze badges
answered Nov 13, 2025 at 9:10
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.