tuto7.htm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. <h2>Generation of the font definition file</h2>
  40. The first step consists in generating a PHP file containing all the information needed by FPDF;
  41. in addition, the font file is compressed. To do this, a helper script is provided in the makefont
  42. directory of the package: makefont.php. It contains the following function:
  43. <br>
  44. <br>
  45. <code>MakeFont(<b>string</b> fontfile, [, <b>string</b> enc [, <b>boolean</b> embed]])</code>
  46. <dl class="param" style="margin-bottom:2em">
  47. <dt><code>fontfile</code></dt>
  48. <dd>
  49. <p>Path to the .ttf, .otf or .pfb file.</p>
  50. </dd>
  51. <dt><code>enc</code></dt>
  52. <dd>
  53. <p>Name of the encoding to use. Default value: <code>cp1252</code>.</p>
  54. </dd>
  55. <dt><code>embed</code></dt>
  56. <dd>
  57. <p>Whether to embed the font or not. Default value: <code>true</code>.</p>
  58. </dd>
  59. </dl>
  60. The first parameter is the name of the font file. The extension must be either .ttf, .otf or .pfb and
  61. determines the font type. If your Type1 font is in ASCII format (.pfa), you can convert it to binary
  62. (.pfb) with the help of <a href="http://www.lcdf.org/~eddietwo/type/#t1utils" target="_blank">t1utils</a>.
  63. <br>
  64. <br>
  65. For Type1 fonts, the corresponding .afm file must be present in the same directory.
  66. <br>
  67. <br>
  68. The encoding defines the association between a code (from 0 to 255) and a character. The first 128 are
  69. always the same and correspond to ASCII; the following are variable. Encodings are stored in .map
  70. files. The available ones are:
  71. <ul>
  72. <li>cp1250 (Central Europe)</li>
  73. <li>cp1251 (Cyrillic)</li>
  74. <li>cp1252 (Western Europe)</li>
  75. <li>cp1253 (Greek)</li>
  76. <li>cp1254 (Turkish)</li>
  77. <li>cp1255 (Hebrew)</li>
  78. <li>cp1257 (Baltic)</li>
  79. <li>cp1258 (Vietnamese)</li>
  80. <li>cp874 (Thai)</li>
  81. <li>ISO-8859-1 (Western Europe)</li>
  82. <li>ISO-8859-2 (Central Europe)</li>
  83. <li>ISO-8859-4 (Baltic)</li>
  84. <li>ISO-8859-5 (Cyrillic)</li>
  85. <li>ISO-8859-7 (Greek)</li>
  86. <li>ISO-8859-9 (Turkish)</li>
  87. <li>ISO-8859-11 (Thai)</li>
  88. <li>ISO-8859-15 (Western Europe)</li>
  89. <li>ISO-8859-16 (Central Europe)</li>
  90. <li>KOI8-R (Russian)</li>
  91. <li>KOI8-U (Ukrainian)</li>
  92. </ul>
  93. Of course, the font must contain the characters corresponding to the chosen encoding.
  94. <br>
  95. <br>
  96. Remark: the standard fonts use cp1252.
  97. <br>
  98. <br>
  99. After you have called the function (create a new file for this and include makefont.php), a .php file
  100. is created, with the same name as the font file. You may rename it if you wish. If the case of embedding,
  101. the font file is compressed and gives a second file with .z as extension (except if the compression
  102. function is not available, it requires Zlib). You may rename it too, but in this case you have to change
  103. the variable <code>$file</code> in the .php file accordingly.
  104. <br>
  105. <br>
  106. Example:
  107. <div class="source">
  108. <pre><code>&lt;?php
  109. <span class="kw">require(</span><span class="str">'makefont/makefont.php'</span><span class="kw">);
  110. </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">);
  111. </span>?&gt;</code></pre>
  112. </div>
  113. which gives the files comic.php and comic.z.
  114. <br>
  115. <br>
  116. Then copy the generated files to the font directory. If the font file could not be compressed, copy
  117. it directly instead of the .z version.
  118. <br>
  119. <br>
  120. Another way to call MakeFont() is through the command line:
  121. <br>
  122. <br>
  123. <kbd>php makefont\makefont.php c:\Windows\Fonts\comic.ttf cp1252</kbd>
  124. <br>
  125. <br>
  126. Finally, for TrueType and OpenType fonts, you can also generate the files
  127. <a href="http://www.fpdf.org/makefont/">online</a> instead of doing it manually.
  128. <h2>Declaration of the font in the script</h2>
  129. The second step is simple. You just need to call the <a href='../doc/addfont.htm'>AddFont()</a> method:
  130. <div class="source">
  131. <pre><code>$pdf<span class="kw">-&gt;</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">);
  132. </span></code></pre>
  133. </div>
  134. And the font is now available (in regular and underlined styles), usable like the others. If we
  135. had worked with Comic Sans MS Bold (comicbd.ttf), we would have written:
  136. <div class="source">
  137. <pre><code>$pdf<span class="kw">-&gt;</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">);
  138. </span></code></pre>
  139. </div>
  140. <h2>Example</h2>
  141. Let's now see a complete example. We will use the font <a href="http://www.abstractfonts.com/font/52" target="_blank">Calligrapher</a>.
  142. The first step is the generation of the font files:
  143. <div class="source">
  144. <pre><code>&lt;?php
  145. <span class="kw">require(</span><span class="str">'makefont/makefont.php'</span><span class="kw">);
  146. </span>MakeFont<span class="kw">(</span><span class="str">'calligra.ttf'</span><span class="kw">,</span><span class="str">'cp1252'</span><span class="kw">);
  147. </span>?&gt;</code></pre>
  148. </div>
  149. The script gives the following report:
  150. <br>
  151. <br>
  152. <b>Warning:</b> character Euro is missing<br>
  153. <b>Warning:</b> character zcaron is missing<br>
  154. Font file compressed: calligra.z<br>
  155. Font definition file generated: calligra.php<br>
  156. <br>
  157. The euro character is not present in the font (it's too old). Another character is missing too.
  158. <br>
  159. <br>
  160. Alternatively we could have used the command line:
  161. <br>
  162. <br>
  163. <kbd>php makefont\makefont.php calligra.ttf cp1252</kbd>
  164. <br>
  165. <br>
  166. or used the online generator.
  167. <br>
  168. <br>
  169. We can now copy the two generated files to the font directory and write the script:
  170. <div class="source">
  171. <pre><code>&lt;?php
  172. <span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
  173. </span>$pdf <span class="kw">= new </span>FPDF<span class="kw">();
  174. </span>$pdf<span class="kw">-&gt;</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">);
  175. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  176. </span>$pdf<span class="kw">-&gt;</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">);
  177. </span>$pdf<span class="kw">-&gt;</span>Write<span class="kw">(</span>10<span class="kw">,</span><span class="str">'Enjoy new fonts with FPDF!'</span><span class="kw">);
  178. </span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
  179. </span>?&gt;</code></pre>
  180. </div>
  181. <p class='demo'><a href='tuto7.php' target='_blank' class='demo'>[Demo]</a></p>
  182. <h2>About the euro symbol</h2>
  183. The euro character is not present in all encodings, and is not always placed at the same position:
  184. <table>
  185. <tr><th>Encoding</th><th>Position</th></tr>
  186. <tr class="alt0"><td>cp1250</td><td>128</td></tr>
  187. <tr class="alt1"><td>cp1251</td><td>136</td></tr>
  188. <tr class="alt0"><td>cp1252</td><td>128</td></tr>
  189. <tr class="alt1"><td>cp1253</td><td>128</td></tr>
  190. <tr class="alt0"><td>cp1254</td><td>128</td></tr>
  191. <tr class="alt1"><td>cp1255</td><td>128</td></tr>
  192. <tr class="alt0"><td>cp1257</td><td>128</td></tr>
  193. <tr class="alt1"><td>cp1258</td><td>128</td></tr>
  194. <tr class="alt0"><td>cp874</td><td>128</td></tr>
  195. <tr class="alt1"><td>ISO-8859-1</td><td>N/A</td></tr>
  196. <tr class="alt0"><td>ISO-8859-2</td><td>N/A</td></tr>
  197. <tr class="alt1"><td>ISO-8859-4</td><td>N/A</td></tr>
  198. <tr class="alt0"><td>ISO-8859-5</td><td>N/A</td></tr>
  199. <tr class="alt1"><td>ISO-8859-7</td><td>N/A</td></tr>
  200. <tr class="alt0"><td>ISO-8859-9</td><td>N/A</td></tr>
  201. <tr class="alt1"><td>ISO-8859-11</td><td>N/A</td></tr>
  202. <tr class="alt0"><td>ISO-8859-15</td><td>164</td></tr>
  203. <tr class="alt1"><td>ISO-8859-16</td><td>164</td></tr>
  204. <tr class="alt0"><td>KOI8-R</td><td>N/A</td></tr>
  205. <tr class="alt1"><td>KOI8-U</td><td>N/A</td></tr>
  206. </table>
  207. ISO-8859-1 is widespread but does not include the euro sign. If you need it, the simplest thing
  208. to do is to use cp1252 or ISO-8859-15 instead, which are nearly identical but contain the precious
  209. symbol.
  210. <h2>Reducing the size of TrueType fonts</h2>
  211. Font files are often quite voluminous; this is due to the fact that they contain the characters
  212. corresponding to many encodings. Zlib compression reduces them but they remain fairly big. A
  213. technique exists to reduce them further. It consists in converting the font to the Type1 format
  214. with <a href="http://ttf2pt1.sourceforge.net" target="_blank">ttf2pt1</a> (the Windows binary is
  215. available <a href="http://www.fpdf.org/fr/dl.php?id=22">here</a>) while specifying the encoding
  216. you are interested in; all other characters will be discarded.
  217. <br>
  218. For example, the arial.ttf font that ships with Windows Vista weights 748 KB (it contains 3381 characters).
  219. After compression it drops to 411. Let's convert it to Type1 by keeping only cp1250 characters:
  220. <br>
  221. <br>
  222. <kbd>ttf2pt1 -b -L cp1250.map c:\Windows\Fonts\arial.ttf arial</kbd>
  223. <br>
  224. <br>
  225. The .map files are located in the makefont directory of the package. The command produces
  226. arial.pfb and arial.afm. The arial.pfb file weights only 57 KB, and 53 after compression.
  227. <br>
  228. <br>
  229. It's possible to go even further. If you are interested only by a subset of the encoding (you
  230. probably don't need all 217 characters), you can open the .map file and remove the lines you are
  231. not interested in. This will reduce the file size accordingly.
  232. </body>
  233. </html>