About Fonts and Character Encoding
The PDF specification requires that PDF readers support a core set of fonts. These fonts are as follows:
- Courier (Normal, Bold, Oblique, and BoldOblique variants)
- Helvetica (Normal, Bold, Oblique, and BoldOblique variants)
- Times (Normal, Bold, Oblique, and BoldOblique variants)
- Symbol
- ZapfDingbats
These fonts only support Windows ANSI encoding. In order for a PDF to
display characters that are not available in Windows ANSI you must
supply an external font, which will be embedded in the PDF. dompdf will
embed any referenced true-type font in the PDF that has been pre-loaded
or is referenced in a CSS @font-face
rule.
Dompdf supports the same fonts as the underlying R&OS PDF class: Type 1 (.pfb) and TrueType (.ttf) so long as the font metrics (.afm/.ufm) are available. The bundled, PHP-based php-font-lib provides support for loading and sub-setting fonts.
As of dompdf 0.6.0 the DejaVu TrueType fonts
have been pre-installed to give dompdf decent Unicode character coverage
by default. To use the DejaVu fonts remember to reference the font in your
stylesheet, e.g. body { font-family: DejaVu Sans; }
(for DejaVu Sans).
Here's an example on how to load a custom font.
use Dompdf\Dompdf;
require '../vendor/autoload.php';
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Tangerine&display=swap" rel="stylesheet" />
<style>
.m {
font-family: 'Montserrat';
}
.t {
font-family: 'Tangerine';
}
</style>
</head>
<body>
<p class="m">
Montserrat
</p>
<p class="t">
Tangerine
</p>
</body>
</html>
HTML
;
//$_dompdf_show_warnings = true;
//$_dompdf_debug = true;
$tmp = sys_get_temp_dir();
$dompdf = new Dompdf([
'logOutputFile' => '',
// authorize DomPdf to download fonts and other Internet assets
'isRemoteEnabled' => true,
// all directories must exist and not end with /
'fontDir' => $tmp,
'fontCache' => $tmp,
'tempDir' => $tmp,
'chroot' => $tmp,
]);
$dompdf->loadHtml($html); //load an html
$dompdf->render();
$dompdf->stream('hello.pdf', [
'compress' => true,
'Attachment' => false,
]);`