Help Getting Font Family Working in Windows

394 views
Skip to first unread message

Marc J. Cawood

unread,
Jul 12, 2009, 1:26:55 PM7/12/09
to dompdf
I really struggled to find out why my stylesheets were being ignored
until I realised you have to install fonts. Apparently basic fonts
like Arial are not included and the load_font.php script is tailor-
made for Linux but not Windows. So, here is how you install the arial
font - make a new batch file with the following contents:

SET TT="C:\Program Files\TtfPt1\bin\ttf2pt1.exe"
SET FF=C:\Windows\Fonts\
SET DP=C:\Inetpub\wwwroot\kb\custom\dompdf
%TT% -a -GAef -OW %FF%arial.ttf %DP%\lib\fonts\arial
%TT% -a -GAef -OW %FF%arialbd.ttf %DP%\lib\fonts\arial-Bold
%TT% -a -GAef -OW %FF%ariali.ttf %DP%\lib\fonts\arial-Italic
%TT% -a -GAef -OW %FF%arialbi.ttf %DP%\lib\fonts\arial-BoldItalic
PAUSE

You should adapt the first 3 lines to point to you relevant file
locations. You can download TtfPt1 here -> http://tinyurl.com/mmrzbd

Now, run this batch file and it should create 4 new .afm files in your
DOMPDF/lib/fonts folder called arial*.afm.

BrianS

unread,
Jul 12, 2009, 11:28:40 PM7/12/09
to dompdf
On Jul 12, 1:26 pm, "Marc J. Cawood" <cawo...@gmail.com> wrote:
> I really struggled to find out why my stylesheets were being ignored
> until I realised you have to install fonts. Apparently basic fonts
> like Arial are not included.

That is true. The only fonts that are supported out of the box are
Helvetica, Times-Roman, Courier, Symbol, & Zapf-Dingbats. These are
required by the PDF spec for readers. I would just like to remind
everyone that when you specify a font you should always use a generic
fallback (e.g. font-family: Arial, sans-serif).

> ... the load_font.php script is tailor-made for Linux but not
> Windows. So, here is how you install the arial font - make a new
> batch file with the following contents:
>
> SET TT="C:\Program Files\TtfPt1\bin\ttf2pt1.exe"
> SET FF=C:\Windows\Fonts\
> SET DP=C:\Inetpub\wwwroot\kb\custom\dompdf
> %TT% -a -GAef -OW  %FF%arial.ttf %DP%\lib\fonts\arial
> %TT% -a -GAef -OW  %FF%arialbd.ttf %DP%\lib\fonts\arial-Bold
> %TT% -a -GAef -OW  %FF%ariali.ttf %DP%\lib\fonts\arial-Italic
> %TT% -a -GAef -OW  %FF%arialbi.ttf %DP%\lib\fonts\arial-BoldItalic
> PAUSE
>
> You should adapt the first 3 lines to point to you relevant file
> locations. You can download TtfPt1 here ->http://tinyurl.com/mmrzbd
>
> Now, run this batch file and it should create 4 new .afm files in your
> DOMPDF/lib/fonts folder called arial*.afm.

Thanks for this info. Two more steps that are not listed above:
1) You have to modify dompdf/lib/fonts/dompdf_font_family_cache so
that the AFM files have valid references. If you're at all familiar
with PHP you should be able to make the appropriate modifications.
2) Ensure that the actual font files are also in the dompdf/lib/fonts
directory and match the names of the .afm files. If they're not DOMPDF
won't be able to find the fonts and the font-embedding won't work.

I would also note that there are some comments on the wiki about how
to get font loading working, see http://code.google.com/p/dompdf/wiki/Installation.
The DOMPDF team will work out some instructions from the information
that's been posted to make sure that Windows users have some
documentation on how to add new fonts.

Moving forward, the next release of DOMPDF should include a Windows
executable for handling font loading (ttf2ufm). This is a modified
version of ttf2pt1 that will help enable UTF-8 support. We're also
considering creating a web-based tool to handle the creation of the
font metric files (.afm). If people are interested in this let us
know.
-b

Marc J. Cawood

unread,
Jul 13, 2009, 2:31:16 AM7/13/09
to dompdf
Correction, it seems you must also run load_font.php to generate the
font cache. This will also copy the fonts. This is the new version

SET TT="C:\Program Files\Ttf2Pt1\bin\ttf2pt1.exe"
SET FF=C:\Windows\Fonts\
SET DP=C:\dompdf

ECHO %DP%

C:\PHP\php.exe -f %DP%\load_font.php arial %FF%\arial.ttf %FF%
\arialbd.ttf %FF%\ariali.ttf %FF%\arialbi.ttf

Marc J. Cawood

unread,
Jul 13, 2009, 2:35:14 AM7/13/09
to dompdf
I am getting the following warnings when working with arial:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
3230
PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
3230
PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
2702
PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
2702
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
The HTML is
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
<html>
<head>
<link rel="stylesheet" type="text/css" href="pdf.css"/>
<title>Content Pasted from MS WORD</title>
</head>
<body><h1><span>Hello World</span></h1>
<h2><span>Install</span></h2>
<p><span>Unzip package to</span></p>
</body>
</html>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
body {font-family: arial; font-size: 11px;}
hr { border: 1px; width: 80%; color: #eee;}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -

When I change "arial" to "helvetica" in the css it works.

dompdf_font_family_cache contains the lines:
'arial' =>
array (
'normal' => 'E:\\dompdf/lib/fonts/arial',
'bold' => 'E:\\dompdf/lib/fonts/arialbd',
'italic' => 'E:\\dompdf/lib/fonts/ariali',
'bold_italic' => 'E:\\dompdf/lib/fonts/arialbi',
),

BrianS

unread,
Jul 13, 2009, 12:53:16 PM7/13/09
to dompdf
On Jul 13, 2:31 am, "Marc J. Cawood" <cawo...@gmail.com> wrote:
> Correction, it seems you must also run load_font.php to generate the
> font cache. This will also copy the fonts. This is the new version

You don't *have* to use load_font.php if you are generating the .afm
files using your method. You would, however, have to manually copy the
actual font files and modify the dompdf_font_family_cache so that it
has accurate references. Also, in order to run load_font.php you need
to ensure that you have set the value of TTF2AFM in
dompdf_config.inc.php so that it points to your copy of ttf2pt1.exe.

> %TT% -a -GAef -OW  "%FF%arial.ttf" "%DP%\lib\fonts\arial"
> %TT% -a -GAef -OW  "%FF%arialbd.ttf" "%DP%\lib\fonts\arial-Bold"
> %TT% -a -GAef -OW  "%FF%ariali.ttf" "%DP%\lib\fonts\arial-Italic"
> %TT% -a -GAef -OW  "%FF%arialbi.ttf" "%DP%\lib\fonts\arial-BoldItalic"

As I mentioned earlier, your .afm file names should be the same as the
font name, otherwise you will encounter embedding problems. So, for
example, the above lines should actually read:

%TT% -a -GAef -OW "%FF%arial.ttf" "%DP%\lib\fonts\arial"
%TT% -a -GAef -OW "%FF%arialbd.ttf" "%DP%\lib\fonts\arialb"
%TT% -a -GAef -OW "%FF%ariali.ttf" "%DP%\lib\fonts\ariali"
%TT% -a -GAef -OW "%FF%arialbi.ttf" "%DP%\lib\fonts\arialbi"

All that being said, I believe that if you correctly set TTF2AFM then
you may not need to worry about the batch file. Again, see the notes
on the wiki page to ensure you don't run into problems.

BrianS

unread,
Jul 13, 2009, 1:16:53 PM7/13/09
to dompdf
On Jul 13, 2:35 am, "Marc J. Cawood" <cawo...@gmail.com> wrote:
> I am getting the following warnings when working with arial:
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - - -
> PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
> 3230
> PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
> 3230
> PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
> 2702
> PHP Notice: Undefined index: in ...\dompdf\lib\class.pdf.php on line
> 2702
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - - -
> dompdf_font_family_cache contains the lines:
>   'arial' =>
>   array (
>     'normal' => 'E:\\dompdf/lib/fonts/arial',
>     'bold' => 'E:\\dompdf/lib/fonts/arialbd',
>     'italic' => 'E:\\dompdf/lib/fonts/ariali',
>     'bold_italic' => 'E:\\dompdf/lib/fonts/arialbi',
>   ),

What is happening here is that DOMPDF has a mapping for arial, but
when it goes to look for the .afm files it can not find them because
they aren't named as expected. The problem goes back to what I said
about the font file names (*.ttf) and the font metric file names
(*.afm) having to match up. See my previous post.

Marc J. Cawood

unread,
Jul 17, 2009, 7:20:57 AM7/17/09
to dompdf
I've given up and am using Georgia font until someone complains ;-)
Reply all
Reply to author
Forward
0 new messages