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';
}
1 Answer 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.
fontCacheandfontDir, 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.