| 1 |
lars |
1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
|
2 |
<html>
|
|
|
3 |
<head>
|
|
|
4 |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
|
5 |
<title>Adding new fonts and encoding support</title>
|
|
|
6 |
<link type="text/css" rel="stylesheet" href="../fpdf.css">
|
|
|
7 |
<style type="text/css">
|
|
|
8 |
table {border-collapse:collapse; border-style:solid; border-width:2px; border-color:#A0A0A0 #000000 #000000 #A0A0A0}
|
|
|
9 |
table {margin:1.4em 0 1.4em 1em}
|
|
|
10 |
th {background-color:#E0EBFF; color:#900000; text-align:left}
|
|
|
11 |
th, td {border:1px solid #808080; padding:2px 10px}
|
|
|
12 |
tr.alt0 {background-color:#FFFFEE}
|
|
|
13 |
tr.alt1 {background-color:#FFFFE0}
|
|
|
14 |
</style>
|
|
|
15 |
</head>
|
|
|
16 |
<body>
|
|
|
17 |
<h1>Adding new fonts and encoding support</h1>
|
|
|
18 |
This tutorial explains how to use TrueType, OpenType and Type1 fonts so that you are not limited to
|
|
|
19 |
the standard fonts any more. The other benefit is that you can choose the font encoding, which allows
|
|
|
20 |
you to use other languages than the Western ones (the standard fonts having too few available characters).
|
|
|
21 |
<br>
|
|
|
22 |
<br>
|
|
|
23 |
Remark: for OpenType, only the format based on TrueType is supported (not the one based on Type1).
|
|
|
24 |
<br>
|
|
|
25 |
<br>
|
|
|
26 |
There are two ways to use a new font: embedding it in the PDF or not. When a font is not
|
|
|
27 |
embedded, it is searched in the system. The advantage is that the PDF file is lighter; on the other
|
|
|
28 |
hand, if it's not available, a substitution font is used. So it's preferable to ensure that the
|
|
|
29 |
needed font is installed on the client systems. If the file is to be viewed by a large audience,
|
|
|
30 |
it's highly recommended to embed.
|
|
|
31 |
<br>
|
|
|
32 |
<br>
|
|
|
33 |
Adding a new font requires two steps:
|
|
|
34 |
<ul>
|
|
|
35 |
<li>Generation of the font definition file</li>
|
|
|
36 |
<li>Declaration of the font in the script</li>
|
|
|
37 |
</ul>
|
|
|
38 |
For Type1, you need the corresponding AFM file. It's usually provided with the font.
|
|
|
39 |
|
|
|
40 |
<h2>Generation of the font definition file</h2>
|
|
|
41 |
The first step consists in generating a PHP file containing all the information needed by FPDF;
|
|
|
42 |
in addition, the font file is compressed. To do this, a helper script is provided in the makefont
|
|
|
43 |
directory of the package: makefont.php. It contains the following function:
|
|
|
44 |
<br>
|
|
|
45 |
<br>
|
|
|
46 |
<code>MakeFont(<b>string</b> fontfile, [, <b>string</b> enc [, <b>boolean</b> embed]])</code>
|
|
|
47 |
<dl class="param" style="margin-bottom:2em">
|
|
|
48 |
<dt><code>fontfile</code></dt>
|
|
|
49 |
<dd>
|
|
|
50 |
<p>Path to the .ttf, .otf or .pfb file.</p>
|
|
|
51 |
</dd>
|
|
|
52 |
<dt><code>enc</code></dt>
|
|
|
53 |
<dd>
|
|
|
54 |
<p>Name of the encoding to use. Default value: <code>cp1252</code>.</p>
|
|
|
55 |
</dd>
|
|
|
56 |
<dt><code>embed</code></dt>
|
|
|
57 |
<dd>
|
|
|
58 |
<p>Whether to embed the font or not. Default value: <code>true</code>.</p>
|
|
|
59 |
</dd>
|
|
|
60 |
</dl>
|
|
|
61 |
The first parameter is the name of the font file. The extension must be either .ttf, .otf or .pfb and
|
|
|
62 |
determines the font type. If your Type1 font is in ASCII format (.pfa), you can convert it to binary
|
|
|
63 |
(.pfb) with the help of <a href="http://www.lcdf.org/~eddietwo/type/#t1utils" target="_blank">t1utils</a>.
|
|
|
64 |
<br>
|
|
|
65 |
<br>
|
|
|
66 |
For Type1 fonts, the corresponding .afm file must be present in the same directory.
|
|
|
67 |
<br>
|
|
|
68 |
<br>
|
|
|
69 |
The encoding defines the association between a code (from 0 to 255) and a character. The first 128 are
|
|
|
70 |
always the same and correspond to ASCII; the following are variable. Encodings are stored in .map
|
|
|
71 |
files. The available ones are:
|
|
|
72 |
<ul>
|
|
|
73 |
<li>cp1250 (Central Europe)</li>
|
|
|
74 |
<li>cp1251 (Cyrillic)</li>
|
|
|
75 |
<li>cp1252 (Western Europe)</li>
|
|
|
76 |
<li>cp1253 (Greek)</li>
|
|
|
77 |
<li>cp1254 (Turkish)</li>
|
|
|
78 |
<li>cp1255 (Hebrew)</li>
|
|
|
79 |
<li>cp1257 (Baltic)</li>
|
|
|
80 |
<li>cp1258 (Vietnamese)</li>
|
|
|
81 |
<li>cp874 (Thai)</li>
|
|
|
82 |
<li>ISO-8859-1 (Western Europe)</li>
|
|
|
83 |
<li>ISO-8859-2 (Central Europe)</li>
|
|
|
84 |
<li>ISO-8859-4 (Baltic)</li>
|
|
|
85 |
<li>ISO-8859-5 (Cyrillic)</li>
|
|
|
86 |
<li>ISO-8859-7 (Greek)</li>
|
|
|
87 |
<li>ISO-8859-9 (Turkish)</li>
|
|
|
88 |
<li>ISO-8859-11 (Thai)</li>
|
|
|
89 |
<li>ISO-8859-15 (Western Europe)</li>
|
|
|
90 |
<li>ISO-8859-16 (Central Europe)</li>
|
|
|
91 |
<li>KOI8-R (Russian)</li>
|
|
|
92 |
<li>KOI8-U (Ukrainian)</li>
|
|
|
93 |
</ul>
|
|
|
94 |
Of course, the font must contain the characters corresponding to the chosen encoding.
|
|
|
95 |
<br>
|
|
|
96 |
<br>
|
|
|
97 |
Remark: the standard fonts use cp1252.
|
|
|
98 |
<br>
|
|
|
99 |
<br>
|
|
|
100 |
After you have called the function (create a new file for this and include makefont.php), a .php file
|
|
|
101 |
is created, with the same name as the font file. You may rename it if you wish. If the case of embedding,
|
|
|
102 |
the font file is compressed and gives a second file with .z as extension (except if the compression
|
|
|
103 |
function is not available, it requires Zlib). You may rename it too, but in this case you have to change
|
|
|
104 |
the variable <code>$file</code> in the .php file accordingly.
|
|
|
105 |
<br>
|
|
|
106 |
<br>
|
|
|
107 |
Example:
|
|
|
108 |
<div class="source">
|
|
|
109 |
<pre><code><?php
|
|
|
110 |
<span class="kw">require(</span><span class="str">'makefont/makefont.php'</span><span class="kw">);
|
|
|
111 |
|
|
|
112 |
</span>MakeFont<span class="kw">(</span><span class="str">'c:\\Windows\\Fonts\\comic.ttf'</span><span class="kw">,</span><span class="str">'cp1252'</span><span class="kw">);
|
|
|
113 |
</span>?></code></pre>
|
|
|
114 |
</div>
|
|
|
115 |
which gives the files comic.php and comic.z.
|
|
|
116 |
<br>
|
|
|
117 |
<br>
|
|
|
118 |
Then copy the generated files to the font directory. If the font file could not be compressed, copy
|
|
|
119 |
it directly instead of the .z version.
|
|
|
120 |
<br>
|
|
|
121 |
<br>
|
|
|
122 |
Another way to call MakeFont() is through the command line:
|
|
|
123 |
<br>
|
|
|
124 |
<br>
|
|
|
125 |
<kbd>php makefont\makefont.php c:\Windows\Fonts\comic.ttf cp1252</kbd>
|
|
|
126 |
<br>
|
|
|
127 |
<br>
|
|
|
128 |
Finally, for TrueType and OpenType fonts, you can also generate the files
|
|
|
129 |
<a href="http://www.fpdf.org/makefont/">online</a> instead of doing it manually.
|
|
|
130 |
|
|
|
131 |
<h2>Declaration of the font in the script</h2>
|
|
|
132 |
The second step is simple. You just need to call the <a href='../doc/addfont.htm'>AddFont()</a> method:
|
|
|
133 |
<div class="source">
|
|
|
134 |
<pre><code>$pdf<span class="kw">-></span>AddFont<span class="kw">(</span><span class="str">'Comic'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'comic.php'</span><span class="kw">);
|
|
|
135 |
</span></code></pre>
|
|
|
136 |
</div>
|
|
|
137 |
And the font is now available (in regular and underlined styles), usable like the others. If we
|
|
|
138 |
had worked with Comic Sans MS Bold (comicbd.ttf), we would have written:
|
|
|
139 |
<div class="source">
|
|
|
140 |
<pre><code>$pdf<span class="kw">-></span>AddFont<span class="kw">(</span><span class="str">'Comic'</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">,</span><span class="str">'comicbd.php'</span><span class="kw">);
|
|
|
141 |
</span></code></pre>
|
|
|
142 |
</div>
|
|
|
143 |
|
|
|
144 |
<h2>Example</h2>
|
|
|
145 |
Let's now see a complete example. We will use the font <a href="http://www.abstractfonts.com/font/52" target="_blank">Calligrapher</a>.
|
|
|
146 |
The first step is the generation of the font files:
|
|
|
147 |
<div class="source">
|
|
|
148 |
<pre><code><?php
|
|
|
149 |
<span class="kw">require(</span><span class="str">'makefont/makefont.php'</span><span class="kw">);
|
|
|
150 |
|
|
|
151 |
</span>MakeFont<span class="kw">(</span><span class="str">'calligra.ttf'</span><span class="kw">,</span><span class="str">'cp1252'</span><span class="kw">);
|
|
|
152 |
</span>?></code></pre>
|
|
|
153 |
</div>
|
|
|
154 |
The script gives the following report:
|
|
|
155 |
<br>
|
|
|
156 |
<br>
|
|
|
157 |
<b>Warning:</b> character Euro is missing<br>
|
|
|
158 |
<b>Warning:</b> character zcaron is missing<br>
|
|
|
159 |
Font file compressed: calligra.z<br>
|
|
|
160 |
Font definition file generated: calligra.php<br>
|
|
|
161 |
<br>
|
|
|
162 |
The euro character is not present in the font (it's too old). Another character is missing too.
|
|
|
163 |
<br>
|
|
|
164 |
<br>
|
|
|
165 |
Alternatively we could have used the command line:
|
|
|
166 |
<br>
|
|
|
167 |
<br>
|
|
|
168 |
<kbd>php makefont\makefont.php calligra.ttf cp1252</kbd>
|
|
|
169 |
<br>
|
|
|
170 |
<br>
|
|
|
171 |
or used the online generator.
|
|
|
172 |
<br>
|
|
|
173 |
<br>
|
|
|
174 |
We can now copy the two generated files to the font directory and write the script:
|
|
|
175 |
<div class="source">
|
|
|
176 |
<pre><code><?php
|
|
|
177 |
<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
|
|
|
178 |
|
|
|
179 |
</span>$pdf <span class="kw">= new </span>FPDF<span class="kw">();
|
|
|
180 |
</span>$pdf<span class="kw">-></span>AddFont<span class="kw">(</span><span class="str">'Calligrapher'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'calligra.php'</span><span class="kw">);
|
|
|
181 |
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
|
|
|
182 |
</span>$pdf<span class="kw">-></span>SetFont<span class="kw">(</span><span class="str">'Calligrapher'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>35<span class="kw">);
|
|
|
183 |
</span>$pdf<span class="kw">-></span>Write<span class="kw">(</span>10<span class="kw">,</span><span class="str">'Enjoy new fonts with FPDF!'</span><span class="kw">);
|
|
|
184 |
</span>$pdf<span class="kw">-></span>Output<span class="kw">();
|
|
|
185 |
</span>?></code></pre>
|
|
|
186 |
</div>
|
|
|
187 |
<p class='demo'><a href='tuto7.php' target='_blank' class='demo'>[Demo]</a></p>
|
|
|
188 |
|
|
|
189 |
<h2>About the euro symbol</h2>
|
|
|
190 |
The euro character is not present in all encodings, and is not always placed at the same position:
|
|
|
191 |
<table>
|
|
|
192 |
<tr><th>Encoding</th><th>Position</th></tr>
|
|
|
193 |
<tr class="alt0"><td>cp1250</td><td>128</td></tr>
|
|
|
194 |
<tr class="alt1"><td>cp1251</td><td>136</td></tr>
|
|
|
195 |
<tr class="alt0"><td>cp1252</td><td>128</td></tr>
|
|
|
196 |
<tr class="alt1"><td>cp1253</td><td>128</td></tr>
|
|
|
197 |
<tr class="alt0"><td>cp1254</td><td>128</td></tr>
|
|
|
198 |
<tr class="alt1"><td>cp1255</td><td>128</td></tr>
|
|
|
199 |
<tr class="alt0"><td>cp1257</td><td>128</td></tr>
|
|
|
200 |
<tr class="alt1"><td>cp1258</td><td>128</td></tr>
|
|
|
201 |
<tr class="alt0"><td>cp874</td><td>128</td></tr>
|
|
|
202 |
<tr class="alt1"><td>ISO-8859-1</td><td>N/A</td></tr>
|
|
|
203 |
<tr class="alt0"><td>ISO-8859-2</td><td>N/A</td></tr>
|
|
|
204 |
<tr class="alt1"><td>ISO-8859-4</td><td>N/A</td></tr>
|
|
|
205 |
<tr class="alt0"><td>ISO-8859-5</td><td>N/A</td></tr>
|
|
|
206 |
<tr class="alt1"><td>ISO-8859-7</td><td>N/A</td></tr>
|
|
|
207 |
<tr class="alt0"><td>ISO-8859-9</td><td>N/A</td></tr>
|
|
|
208 |
<tr class="alt1"><td>ISO-8859-11</td><td>N/A</td></tr>
|
|
|
209 |
<tr class="alt0"><td>ISO-8859-15</td><td>164</td></tr>
|
|
|
210 |
<tr class="alt1"><td>ISO-8859-16</td><td>164</td></tr>
|
|
|
211 |
<tr class="alt0"><td>KOI8-R</td><td>N/A</td></tr>
|
|
|
212 |
<tr class="alt1"><td>KOI8-U</td><td>N/A</td></tr>
|
|
|
213 |
</table>
|
|
|
214 |
ISO-8859-1 is widespread but does not include the euro sign. If you need it, the simplest thing
|
|
|
215 |
to do is to use cp1252 or ISO-8859-15 instead, which are nearly identical but contain the precious
|
|
|
216 |
symbol.
|
|
|
217 |
|
|
|
218 |
<h2>Reducing the size of TrueType fonts</h2>
|
|
|
219 |
Font files are often quite voluminous; this is due to the fact that they contain the characters
|
|
|
220 |
corresponding to many encodings. Z
|