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:

  1. Use CSS @font-face rules to load a font at run-time.
  2. From the command line use dompdf/load_font.php.
  3. 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.


Dompdf's About Fonts and Character Encoding says the font utility is included but doesn't tell you how to get it and run it. Here's how:

  1. Download load_font.php to the root directory of your project. curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php

  2. Open load_font.php with a text editor (e.g. vim). Change require_once "autoload.inc.php"; to require_once "vendor/autoload.php";

  3. Run the utility with the name of the font you are registering and the path to the TFF file. For example: php load_font.php 'Brush Script MT' https/fonts/brush-script-mt.ttf

Read the code for load_font.php for more information about how to use this command.


I have a similar problem and i have been looking a solution for 2 days...With the new version, the accepted answer does not work any more.

@jay-bienvenu answer is correct. The new version of DomPDF does not include everything and also there is a very poor documentation.

So you will have to:

  1. download load_font.php and place it to the root directory of your project: curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php

  2. then open load_font.php with your editor and place the correct path to your autoload.inc.php, eg require_once 'lib/dompdf/autoload.inc.php';

  3. Open the command line, go to the root folder of your project, and run the utility with the name of the font you are registering and the path to the TFF file eg php load_font.php SourceSansPro ./pathToYourFolder/lib/dompdf/SourceSansPro-Regular.ttf ./pathToYourFolder/lib/dompdf/SourceSansPro-Bold.ttf

Now the font is installed. You may use it as you would normally would as a webfont in html:

<html>
<head>
    <meta http-equiv="Content-Type" content="charset=utf-8" />
    <style type="text/css">
        @page {
            margin: 0;
        }
        * { padding: 0; margin: 0; }
        @font-face {
            font-family: "source_sans_proregular";           
            src: local("Source Sans Pro"), url("fonts/sourcesans/sourcesanspro-regular-webfont.ttf") format("truetype");
            font-weight: normal;
            font-style: normal;

        }        
        body{
            font-family: "source_sans_proregular", Calibri,Candara,Segoe,Segoe UI,Optima,Arial,sans-serif;            
        }
    </style>
</head>
<body>
Your code here ....
</body>
</html>

and to make the pdf in php:

require_once 'lib/dompdf/autoload.inc.php';

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($html, 'UTF-8');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4');

$dompdf->set_option('defaultMediaType', 'all');
$dompdf->set_option('isFontSubsettingEnabled', true);

// Render the HTML as PDF
$dompdf->render();

EDIT: i made a blog post about how to use dompdf and apply custom fonts so that things can be more detailed: https://www.blog.lab21.gr/using-domdpf-create-pdf-php

Tags:

Php

Dompdf