Custom fonts usage

1,599 views
Skip to first unread message

Draakjuh

unread,
Jan 5, 2012, 9:15:52 AM1/5/12
to dompdf
I have a HTML document, but I want to add custom fonts. I followed the
tutorial on the website, but it still doesn't work.

I want to use Calibri font instead. I made of a TTF file also a AFM
file and placed both inside: ../lib/fonts/calibri.ttf and ../lib/fonts/
calibri.afm.
How can I change my html file that it loads the Calibri font correct?

<?php
require_once("../dompdf_config.inc.php");

$html = "
<html> <style>
body, html { font-family: sans-serif; }

</style>

<body>
<p>test test</p>

-Content here-

</body>

</html>";

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("my_pdf.pdf", array("Attachment" => 0)); //
------------------ PDF name ------------------\\

?>

Draakjuh

unread,
Jan 5, 2012, 10:14:10 AM1/5/12
to dompdf
I changed now the dompdf_font_family_cache_dlist.php with the
following lines:

'calibri' => array (
'normal' => DOMPDF_FONT_DIR . 'Calibri',
'bold' => DOMPDF_FONT_DIR . 'Calibri-Bold',
'italic' => DOMPDF_FONT_DIR . 'Calibri-Oblique',
'bold_italic' => DOMPDF_FONT_DIR . 'Calibri-BoldOblique'
)

I added in this map the fonts as AFM files.

WHen I change the font-family, I get an error....

BrianS

unread,
Jan 5, 2012, 1:12:27 PM1/5/12
to dom...@googlegroups.com
On Thursday, January 5, 2012 10:14:10 AM UTC-5, Draakjuh wrote:
I want to use Calibri font instead. I made of a TTF file also a AFM file and placed both inside: ../lib/fonts/calibri.ttf and ../lib/fonts/calibri.afm.
...
I changed now the dompdf_font_family_cache_dlist.php with the following lines:

  'calibri' => array (
    'normal' => DOMPDF_FONT_DIR . 'Calibri',
    'bold' => DOMPDF_FONT_DIR . 'Calibri-Bold',
    'italic' => DOMPDF_FONT_DIR . 'Calibri-Oblique',
    'bold_italic' => DOMPDF_FONT_DIR . 'Calibri-BoldOblique'
  )

Unless you have a TTF (e.g. Calibiri-Bold.ttf, etc.) and AFM/UFM for each of these variants you should use the same mapping for all variants. For example:

'calibri' => array (
  'normal' => DOMPDF_FONT_DIR . 'calibri',
  'bold' => DOMPDF_FONT_DIR . 'calibri',
  'italic' => DOMPDF_FONT_DIR . 'calibri',
  'bold_italic' => DOMPDF_FONT_DIR . 'calibri'
)

How can I change my html file that it loads the Calibri font correct?

The change to your HTML is fairly minor. The following should work (modification highlighted):

<html>
<head>
  <style>
    body { font-family: calibiri, sans-serif; }
  </style>
</head>

<body>
  <p>test test</p>
</body>
</html>

WHen I change the font-family, I get an error....

How did you change the font-family declaration? What kind of error do you get?

Draakjuh

unread,
Jan 9, 2012, 2:23:04 AM1/9/12
to dompdf
I have both the TTF files and AFM files inside the font folder. (I
converted the TTF files at: http://www.files-conversion.com/font-converter.php)

After that I changed the dompdf_font_family_cache_dlist.php with the
following:

'Calibri' => array (
'normal' => DOMPDF_FONT_DIR . 'Calibri',
'bold' => DOMPDF_FONT_DIR . 'Calibri-Bold',
'italic' => DOMPDF_FONT_DIR . 'Calibri-Oblique',
'bold_italic' => DOMPDF_FONT_DIR . 'Calibri-BoldOblique'
)

The TTF and AFM files are also named:
Calibri.ttf, Calibri.afm,
Calibri-Bold.ttf, Calibri-Bold.afm,
Calibri-Oblique.ttf, Calibri-Oblique.afm,
Calibri-BoldOblique.ttf, Calibri-BoldOblique.afm.

After that I changed the font family in the body to: font-family:
Calibri;. Now i didn't get an error, but the font didn't change to
calibri, but instead of that a standard webfont.

Is there something I did wrong now?

Draakjuh

unread,
Jan 9, 2012, 2:52:08 AM1/9/12
to dompdf
Now I tried the converter at:
http://eclecticgeek.com/dompdf/load_font.php

And placed the files inside the /lib/fonts/ folder, but still it does
not change to calibri.

BrianS

unread,
Jan 9, 2012, 10:53:51 AM1/9/12
to dom...@googlegroups.com
On Monday, January 9, 2012 2:23:04 AM UTC-5, Draakjuh wrote:
I have both the TTF files and AFM files inside the font folder. (I
converted the TTF files at: http://www.files-conversion.com/font-converter.php)

After that I changed the dompdf_font_family_cache_dlist.php with the
following:

  'Calibri' => array (
    'normal' => DOMPDF_FONT_DIR . 'Calibri',
    'bold' => DOMPDF_FONT_DIR . 'Calibri-Bold',
    'italic' => DOMPDF_FONT_DIR . 'Calibri-Oblique',
    'bold_italic' => DOMPDF_FONT_DIR . 'Calibri-BoldOblique'
  )

The TTF and AFM files are also named:
Calibri.ttf, Calibri.afm,
Calibri-Bold.ttf, Calibri-Bold.afm,
Calibri-Oblique.ttf, Calibri-Oblique.afm,
Calibri-BoldOblique.ttf, Calibri-BoldOblique.afm.

After that I changed the font family in the body to: font-family:
Calibri;. Now i didn't get an error, but the font didn't change to
calibri, but instead of that a standard webfont.

Is there something I did wrong now?

It sounds like your setup should be OK to display the font as expected. You might need to clear to font cache. Look for files with names similar to Calibri.afm.php and remove them. Can you post a sample of your HTML document so we can look for any problems?

Fabien Ménager

unread,
Jan 11, 2012, 4:18:47 PM1/11/12
to dom...@googlegroups.com
You may need to write 

   'calibri' => array ...


 instead of

   'Calibri' => array ...

I think the keys need to be lower case here.

Draakjuh

unread,
Jan 18, 2012, 7:51:52 AM1/18/12
to dompdf
I got it fixed now, but when I place it on FTP it also works, but with
Codeigniter the font doesn't change.

I added the following line:
'calibri' => array (
'normal' => DOMPDF_FONT_DIR . 'calibri',
'bold' => DOMPDF_FONT_DIR . 'calibri-bold',
'italic' => DOMPDF_FONT_DIR . 'calibri-oblique',
'bold_italic' => DOMPDF_FONT_DIR . 'calibri'
)



Draakjuh

unread,
Jan 19, 2012, 5:05:53 AM1/19/12
to dompdf
I got it fixed in Codeigniter now. For people who will have the same
problem I fixed this by changing the
dompdf_font_family_cache_dlist.php like:

'calibri' => array (
'normal' => DOMPDF_FONT_DIR . 'Calibri',
'bold' => DOMPDF_FONT_DIR . 'Calibri-Bold',
'italic' => DOMPDF_FONT_DIR . 'Calibri-Oblique',
'bold_italic' => DOMPDF_FONT_DIR . 'calibri'
)

So I changed the lower-case letters to upper-case letters.

Cheers
Reply all
Reply to author
Forward
0 new messages