Dompdf and set different font-family
PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol (all using Windows ANSI encoding). dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and referenced in a CSS @font-face
rule. The loading process is necessary in order to produce the font metrics used for type setting.
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.
The process for loading a font varies depending on your needs and server access. There are three ways you can load a font:
- Use CSS @font-face rules to load a font at run-time.
- From the command line use dompdf/load_font.php.
- Browse to dompdf/www/fonts.php in the included admin site.
Use CSS @font-face rules to load a font at run-time
No command line access required. So long as the font you want to load is available online you can load it easily via CSS.
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: normal;
src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format('truetype');
}
From the command line use dompdf/load_font.php
If you have access to the command line then loading a font is as simple as:
[php] load_font.php "NeutraText-Book" /path/to/neutratext.ttf
Run the command without any parameters to see help text. Quickly, though, the parameters are: name of the font, normal font file, bold font file, italic font file, bold-italic font file
Browse to dompdf/www/fonts.php in the included admin site
Self-explanatory (sample). The only thing you need to do is make sure you've modified the admin username/password combo
Note: load_font.php and the admin site will not be included by default starting with dompdf 0.7.0
Adapted from the dompdf wiki (Unicode How-To, About Fonts and Character Encoding) and other sources.
Custom Fonts in DomPDF
This ended up being a permissions issue. Once I changed the permissions on the DomPDF library folder to make it writeable, my fonts started working as expected.
Custom Fonts for DOMPDF
- go to your DOMPDF folder
- copy your font's as .ttf (TrueType Font) or .otf (OpenType Font) into the DOMPDF's root
- open your command line and run
php load_font.php your_fonts_name ./your-normal.ttf ./your-bold.ttf ./your-bold-italic.ttf
- DOMPDF now created Adobe Font Metrics and copied it to lib/fonts/* - you can now use it with
font-family: your_fonts_name;
Related Topics
Convert Stdclass Object to Array in PHP
PHP String Concatenation, Performance
PHP List All Files in Directory
Why Are My PHP Files Showing as Plain Text
Transform Relative Path into Absolute Url Using PHP
Sort PHP Multi-Dimensional Array Based on Value in Inner Array
PHP Sessions That Have Already Been Started
Cannot Connect to MySQL 4.1+ Using Old Authentication
Pass a Variable to a PHP Script Running from the Command Line
Convert String to Binary Then Back Again Using PHP
Date Difference in PHP on Days