Skip to content
Snippets Groups Projects

Update for unicode fonts

Merged DaveD requested to merge DaveD/installation:font into master
+ 22
44
# Using non-latin characters in PDF output
# Using your own font or CJK characters in PDF output
There are certain places in CiviCRM where PDF output will contain `?` instead of the appropriate character because the DejaVu unicode font files have been removed from the DOMPDF library. For example CiviCRM uses DOMPDF when producing PDF copies of CiviReports, but it uses a different library (TCPDF) for Mailing Labels, which does have the DejaVu fonts.
As of CiviCRM 6.0, most unicode characters will output correctly in PDFs. But if you need CJK characters DejaVu does not contain those, so you will need to install another font file such as SimSun using the steps below.
In Drupal 9, the entire DOMPDF library is downloaded during install, so it will work with most unicode characters as-is. But if you need CJK characters DejaVu does not contain those, so you will need to install another font file such as SimSun using the steps below.
If you are still seeing question marks (?) in place of unicode characters, even for non-CJK characters, check that you don't have a `<font>` tag or `style` attribute in the html specifically setting a font that doesn't support unicode.
Note when testing to see if you've succeeded, use letters that are entirely outside the character set included in the basic font files, e.g. use cyrillic letters such as `д`. Several letters like `ü` will work either way so aren't a good test.
## Option A: Using web fonts
??? example "Install the unicode font files for DOMPDF"
1. In your html template add a `<link>` tag like the following, e.g. for the Noto Sans SC font: `<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100..900">`
1. Specify the font using CSS in the templates where you use the CJK characters. You can use the Source button in CKEditor to do this and type it directly into the HTML: e.g. `<p style="font-family: Noto Sans SC;">的</p>`
Note - except for CJK character support below these steps are done for you if you install [dompdf-fonts](https://github.com/eileenmcnaughton/dompdf-fonts)
## Option B: Storing fonts locally
1. Download the dompdf zip file from the [github repository](https://github.com/dompdf/dompdf/releases).
1. Create a folder in a location that won't get overwritten when you upgrade, e.g. sites/default/fonts for Drupal.
1. Extract all the DejaVu files, including the DejaVu-XXX.ufm files as well as the .ttf files, from the lib/fonts subfolder in the zip file into the folder you created.
1. In the folder you created, create a file called `dompdf_font_family_cache.php` and put this in it. Don't replace `$fontDir` with anything leave it as that variable.
1. While option A will cache a version of the file after the first download, it may not be available on the internet or may be a commercial font.
1. Create a folder in a location that won't get overwritten when you upgrade, e.g. sites/default/fonts for Drupal.
1. Copy the .ttf font file you want to use into that folder.
1. Run `php -r 'require_once "/path/to/civicrm/vendor/autoload.php"; $f = \FontLib\Font::load("/full/path/to/the/ttf/file"); $f->parse(); $f->saveAdobeFontMetrics("/full/path/to/output/ufm/file");'`
* e.g. `php -r 'require_once "/var/www/sites/all/modules/civicrm/vendor/autoload.php"; $f = \FontLib\Font::load("/var/www/sites/default/fonts/simsun.ttf"); $f->parse(); $f->saveAdobeFontMetrics("/var/www/sites/default/fonts/simsun.ufm");'`
1. Create a file in that folder called `installed-fonts.json` that looks like this:
```json
{
"simsun": {
"normal": "simsun"
}
}
```
<?php return [
'dejavu sans' => [
'bold' => $fontDir . '/DejaVuSans-Bold',
'bold_italic' => $fontDir . '/DejaVuSans-BoldOblique',
'italic' => $fontDir . '/DejaVuSans-Oblique',
'normal' => $fontDir . '/DejaVuSans',
],
'dejavu sans mono' => [
'bold' => $fontDir . '/DejaVuSansMono-Bold',
'bold_italic' => $fontDir . '/DejaVuSansMono-BoldOblique',
'italic' => $fontDir . '/DejaVuSansMono-Oblique',
'normal' => $fontDir . '/DejaVuSansMono',
],
'dejavu serif' => [
'bold' => $fontDir . '/DejaVuSerif-Bold',
'bold_italic' => $fontDir . '/DejaVuSerif-BoldItalic',
'italic' => $fontDir . '/DejaVuSerif-Italic',
'normal' => $fontDir . '/DejaVuSerif',
],
];
```
1. Visit Administer - System Settings - Misc and in the field for `DOMPDF Font Folder` put the full path to the font folder you created.
1. When composing the PDF, if you have pasted from Microsoft Word into CKEditor it might set a specific font and then you will still see `?` in the output. You can click the Source button on the CKEditor toolbar to check - look for e.g. `<p style="font-family: Arial;">`.
## Additional steps for CJK or an alternate unicode font
1. You will need to use the `load_font` utility from https://github.com/dompdf/utils. Download and extract it into some temporary folder.
1. Edit [the require line](https://github.com/dompdf/utils/blob/d9049f5b5df23b4e97b332a81205b5bb5ae1fbcc/load_font.php#L3) near the top of the `load_font.php` file and set it to the path to your CiviCRM installation's `vendor/autoload.php`.
1. Uncomment and edit [the $fontDir line](https://github.com/dompdf/utils/blob/d9049f5b5df23b4e97b332a81205b5bb5ae1fbcc/load_font.php#L9) near the top of the `load_font.php` file and set it to the fonts folder you created earlier for DejaVu.
1. Copy the .ttf font file you want to use into the folder where `load_font.php` is.
1. Run `php load_font.php font_name font_file.ttf`, where you replace the last two as appropriate for the font you are installing, e.g. `php load_font.php SimSun simsun.ttf`.
1. Note this will overwrite the `dompdf_font_family_cache.php` file you created earlier for DejaVu, so to also have DejaVu you will now need to edit the file and copy the php array from earlier back in.
1. Now you no longer need the `load_font` utility or its folder and can delete it.
1. Unlike with DejaVu which CiviCRM sets as the default font, you will need to specify the font when composing the PDF in the places where you use the CJK characters. You can use the Source button in CKEditor to do this and type it directly into the HTML: e.g. `<p style="font-family: simsun;">的</p>`
1. Visit Administer - System Settings - Misc and in the field for DOMPDF Font Folder put the full path to the font folder you created in step 2.
1. Specify the font using CSS in the templates where you use the CJK characters. You can use the Source button in CKEditor to do this and type it directly into the HTML: e.g. `<p style="font-family: simsun;">的</p>`
1. Note any fonts you set up here are in addition to the ones distributed with dompdf/CiviCRM. You do not need to copy those core fonts over or add entries in this json file to use them.
Loading