FAQ.htm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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>FAQ</title>
  6. <link type="text/css" rel="stylesheet" href="fpdf.css">
  7. <style type="text/css">
  8. ul {list-style-type:none; margin:0; padding:0}
  9. ul#answers li {margin-top:1.8em}
  10. .question {font-weight:bold; color:#900000}
  11. </style>
  12. </head>
  13. <body>
  14. <h1>FAQ</h1>
  15. <ul>
  16. <li><b>1.</b> <a href='#q1'>What's exactly the license of FPDF? Are there any usage restrictions?</a></li>
  17. <li><b>2.</b> <a href='#q2'>When I try to create a PDF, a lot of weird characters show on the screen. Why?</a></li>
  18. <li><b>3.</b> <a href='#q3'>I try to generate a PDF and IE displays a blank page. What happens?</a></li>
  19. <li><b>4.</b> <a href='#q4'>I can't make line breaks work. I put \n in the string printed by MultiCell but it doesn't work.</a></li>
  20. <li><b>5.</b> <a href='#q5'>I try to display a variable in the Header method but nothing prints.</a></li>
  21. <li><b>6.</b> <a href='#q6'>I defined the Header and Footer methods in my PDF class but nothing appears.</a></li>
  22. <li><b>7.</b> <a href='#q7'>Accented characters are replaced by some strange characters like é.</a></li>
  23. <li><b>8.</b> <a href='#q8'>I try to display the Euro symbol but it doesn't work.</a></li>
  24. <li><b>9.</b> <a href='#q9'>I get the following error when I try to generate a PDF: Some data has already been output, can't send PDF file</a></li>
  25. <li><b>10.</b> <a href='#q10'>I draw a frame with very precise dimensions, but when printed I notice some differences.</a></li>
  26. <li><b>11.</b> <a href='#q11'>I'd like to use the whole surface of the page, but when printed I always have some margins. How can I get rid of them?</a></li>
  27. <li><b>12.</b> <a href='#q12'>How can I put a background in my PDF?</a></li>
  28. <li><b>13.</b> <a href='#q13'>How can I set a specific header or footer on the first page?</a></li>
  29. <li><b>14.</b> <a href='#q14'>I'd like to use extensions provided by different scripts. How can I combine them?</a></li>
  30. <li><b>15.</b> <a href='#q15'>How can I send the PDF by email?</a></li>
  31. <li><b>16.</b> <a href='#q16'>What's the limit of the file sizes I can generate with FPDF?</a></li>
  32. <li><b>17.</b> <a href='#q17'>Can I modify a PDF with FPDF?</a></li>
  33. <li><b>18.</b> <a href='#q18'>I'd like to make a search engine in PHP and index PDF files. Can I do it with FPDF?</a></li>
  34. <li><b>19.</b> <a href='#q19'>Can I convert an HTML page to PDF with FPDF?</a></li>
  35. <li><b>20.</b> <a href='#q20'>Can I concatenate PDF files with FPDF?</a></li>
  36. </ul>
  37. <ul id='answers'>
  38. <li id='q1'>
  39. <p><b>1.</b> <span class='question'>What's exactly the license of FPDF? Are there any usage restrictions?</span></p>
  40. FPDF is released under a permissive license: there is no usage restriction. You may embed it
  41. freely in your application (commercial or not), with or without modifications.
  42. </li>
  43. <li id='q2'>
  44. <p><b>2.</b> <span class='question'>When I try to create a PDF, a lot of weird characters show on the screen. Why?</span></p>
  45. These "weird" characters are in fact the actual content of your PDF. This behavior is a bug of
  46. IE6. When it first receives an HTML page, then a PDF from the same URL, it displays it directly
  47. without launching Acrobat. This happens frequently during the development stage: on the least
  48. script error, an HTML page is sent, and after correction, the PDF arrives.
  49. <br>
  50. To solve the problem, simply quit and restart IE. You can also go to another URL and come
  51. back.
  52. <br>
  53. To avoid this kind of inconvenience during the development, you can generate the PDF directly
  54. to a file and open it through the explorer.
  55. </li>
  56. <li id='q3'>
  57. <p><b>3.</b> <span class='question'>I try to generate a PDF and IE displays a blank page. What happens?</span></p>
  58. First of all, check that you send nothing to the browser after the PDF (not even a space or a
  59. carriage return). You can put an exit statement just after the call to the Output() method to
  60. be sure. If it still doesn't work, it means you're a victim of the "blank page syndrome". IE
  61. used in conjunction with the Acrobat plug-in suffers from many bugs. To avoid these problems
  62. in a reliable manner, two main techniques exist:
  63. <br>
  64. <br>
  65. - Disable the plug-in and use Acrobat as a helper application. To do this, launch Acrobat, go
  66. to the Edit menu, Preferences, Internet, and uncheck "Display PDF in browser". Then, the next
  67. time you load a PDF in IE, it displays the dialog box "Open it" or "Save it to disk". Uncheck
  68. the option "Always ask before opening this type of file" and choose Open. From now on, PDF files
  69. will open automatically in an external Acrobat window.
  70. <br>
  71. The drawback of the method is that you need to alter the client configuration, which you can do
  72. in an intranet environment but not for the Internet.
  73. <br>
  74. <br>
  75. - Use a redirection technique. It consists in generating the PDF in a temporary file on the server
  76. and redirect the client to it. For example, at the end of the script, you can put the following:
  77. <div class="doc-source">
  78. <pre><code>//Determine a temporary file name in the current directory
  79. $file = basename(tempnam('.', 'tmp'));
  80. rename($file, $file.'.pdf');
  81. $file .= '.pdf';
  82. //Save PDF to file
  83. $pdf-&gt;Output($file, 'F');
  84. //Redirect
  85. header('Location: '.$file);</code></pre>
  86. </div>
  87. This method turns the dynamic PDF into a static one and avoids all troubles. But you have to do
  88. some cleaning in order to delete the temporary files. For example:
  89. <div class="doc-source">
  90. <pre><code>function CleanFiles($dir)
  91. {
  92. //Delete temporary files
  93. $t = time();
  94. $h = opendir($dir);
  95. while($file=readdir($h))
  96. {
  97. if(substr($file,0,3)=='tmp' &amp;&amp; substr($file,-4)=='.pdf')
  98. {
  99. $path = $dir.'/'.$file;
  100. if($t-filemtime($path)&gt;3600)
  101. @unlink($path);
  102. }
  103. }
  104. closedir($h);
  105. }</code></pre>
  106. </div>
  107. This function deletes all files of the form tmp*.pdf older than an hour in the specified
  108. directory. You may call it where you want, for example in the script which generates the PDF.
  109. </li>
  110. <li id='q4'>
  111. <p><b>4.</b> <span class='question'>I can't make line breaks work. I put \n in the string printed by MultiCell but it doesn't work.</span></p>
  112. You have to enclose your string with double quotes, not single ones.
  113. </li>
  114. <li id='q5'>
  115. <p><b>5.</b> <span class='question'>I try to display a variable in the Header method but nothing prints.</span></p>
  116. You have to use the <code>global</code> keyword to access global variables, for example:
  117. <div class="doc-source">
  118. <pre><code>function Header()
  119. {
  120. global $title;
  121. $this-&gt;SetFont('Arial', 'B', 15);
  122. $this-&gt;Cell(0, 10, $title, 1, 1, 'C');
  123. }
  124. $title = 'My title';</code></pre>
  125. </div>
  126. Alternatively, you can use an object property:
  127. <div class="doc-source">
  128. <pre><code>function Header()
  129. {
  130. $this-&gt;SetFont('Arial', 'B', 15);
  131. $this-&gt;Cell(0, 10, $this-&gt;title, 1, 1, 'C');
  132. }
  133. $pdf-&gt;title = 'My title';</code></pre>
  134. </div>
  135. </li>
  136. <li id='q6'>
  137. <p><b>6.</b> <span class='question'>I defined the Header and Footer methods in my PDF class but nothing appears.</span></p>
  138. You have to create an object from the PDF class, not FPDF:
  139. <div class="doc-source">
  140. <pre><code>$pdf = new PDF();</code></pre>
  141. </div>
  142. </li>
  143. <li id='q7'>
  144. <p><b>7.</b> <span class='question'>Accented characters are replaced by some strange characters like é.</span></p>
  145. Don't use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252.
  146. It is possible to perform a conversion to ISO-8859-1 with utf8_decode():
  147. <div class="doc-source">
  148. <pre><code>$str = utf8_decode($str);</code></pre>
  149. </div>
  150. But some characters such as Euro won't be translated correctly. If the iconv extension is available, the
  151. right way to do it is the following:
  152. <div class="doc-source">
  153. <pre><code>$str = iconv('UTF-8', 'windows-1252', $str);</code></pre>
  154. </div>
  155. </li>
  156. <li id='q8'>
  157. <p><b>8.</b> <span class='question'>I try to display the Euro symbol but it doesn't work.</span></p>
  158. The standard fonts have the Euro character at position 128. You can define a constant like this
  159. for convenience:
  160. <div class="doc-source">
  161. <pre><code>define('EURO', chr(128));</code></pre>
  162. </div>
  163. </li>
  164. <li id='q9'>
  165. <p><b>9.</b> <span class='question'>I get the following error when I try to generate a PDF: Some data has already been output, can't send PDF file</span></p>
  166. You must send nothing to the browser except the PDF itself: no HTML, no space, no carriage return. A common
  167. case is having extra blank at the end of an included script file.<br>
  168. If you can't figure out where the problem comes from, this other message appearing just before can help you:<br>
  169. <br>
  170. <b>Warning:</b> Cannot modify header information - headers already sent by (output started at script.php:X)<br>
  171. <br>
  172. It means that script.php outputs something at line X. Go to this line and fix it.
  173. In case the message doesn't show, first check that you didn't disable warnings, then add this at the very
  174. beginning of your script:
  175. <div class="doc-source">
  176. <pre><code>ob_end_clean();</code></pre>
  177. </div>
  178. If you still don't see it, disable zlib.output_compression in your php.ini and it should appear.
  179. </li>
  180. <li id='q10'>
  181. <p><b>10.</b> <span class='question'>I draw a frame with very precise dimensions, but when printed I notice some differences.</span></p>
  182. To respect dimensions, select "None" for the Page Scaling setting instead of "Shrink to Printable Area" in the print dialog box.
  183. </li>
  184. <li id='q11'>
  185. <p><b>11.</b> <span class='question'>I'd like to use the whole surface of the page, but when printed I always have some margins. How can I get rid of them?</span></p>
  186. Printers have physical margins (different depending on the models); it is therefore impossible to remove
  187. them and print on the whole surface of the paper.
  188. </li>
  189. <li id='q12'>
  190. <p><b>12.</b> <span class='question'>How can I put a background in my PDF?</span></p>
  191. For a picture, call Image() in the Header() method, before any other output. To set a background color, use Rect().
  192. </li>
  193. <li id='q13'>
  194. <p><b>13.</b> <span class='question'>How can I set a specific header or footer on the first page?</span></p>
  195. Simply test the page number:
  196. <div class="doc-source">
  197. <pre><code>function Header()
  198. {
  199. if($this-&gt;PageNo()==1)
  200. {
  201. //First page
  202. ...
  203. }
  204. else
  205. {
  206. //Other pages
  207. ...
  208. }
  209. }</code></pre>
  210. </div>
  211. </li>
  212. <li id='q14'>
  213. <p><b>14.</b> <span class='question'>I'd like to use extensions provided by different scripts. How can I combine them?</span></p>
  214. Use an inheritance chain. If you have two classes, say A in a.php:
  215. <div class="doc-source">
  216. <pre><code>require('fpdf.php');
  217. class A extends FPDF
  218. {
  219. ...
  220. }</code></pre>
  221. </div>
  222. and B in b.php:
  223. <div class="doc-source">
  224. <pre><code>require('fpdf.php');
  225. class B extends FPDF
  226. {
  227. ...
  228. }</code></pre>
  229. </div>
  230. then make B extend A:
  231. <div class="doc-source">
  232. <pre><code>require('a.php');
  233. class B extends A
  234. {
  235. ...
  236. }</code></pre>
  237. </div>
  238. and make your own class extend B:
  239. <div class="doc-source">
  240. <pre><code>require('b.php');
  241. class PDF extends B
  242. {
  243. ...
  244. }
  245. $pdf = new PDF();</code></pre>
  246. </div>
  247. </li>
  248. <li id='q15'>
  249. <p><b>15.</b> <span class='question'>How can I send the PDF by email?</span></p>
  250. As any other file, but an easy way is to use <a href="http://phpmailer.codeworxtech.com">PHPMailer</a> and
  251. its in-memory attachment:
  252. <div class="doc-source">
  253. <pre><code>$mail = new PHPMailer();
  254. ...
  255. $doc = $pdf-&gt;Output('', 'S');
  256. $mail-&gt;AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf');
  257. $mail-&gt;Send();</code></pre>
  258. </div>
  259. </li>
  260. <li id='q16'>
  261. <p><b>16.</b> <span class='question'>What's the limit of the file sizes I can generate with FPDF?</span></p>
  262. There is no particular limit. There are some constraints, however:
  263. <br>
  264. <br>
  265. - The maximum memory size allocated to PHP scripts is usually 8MB. For very big documents,
  266. especially with images, this limit may be reached (the file being built into memory). The
  267. parameter is configured in the php.ini file.
  268. <br>
  269. <br>
  270. - The maximum execution time allocated defaults to 30 seconds. This limit can of course be easily
  271. reached. It is configured in php.ini and may be altered dynamically with set_time_limit().
  272. <br>
  273. <br>
  274. - Browsers generally have a 5 minute time-out. If you send the PDF directly to the browser and
  275. reach the limit, it will be lost. It is therefore advised for very big documents to
  276. generate them in a file, and to send some data to the browser from time to time (with a call
  277. to flush() to force the output). When the document is finished, you can send a redirection to
  278. it or create a link.
  279. <br>
  280. Remark: even if the browser times out, the script may continue to run on the server.
  281. </li>
  282. <li id='q17'>
  283. <p><b>17.</b> <span class='question'>Can I modify a PDF with FPDF?</span></p>
  284. It is possible to import pages from an existing PDF document thanks to the FPDI extension:<br>
  285. <br>
  286. <a href="http://www.setasign.de/products/pdf-php-solutions/fpdi/" target="_blank">http://www.setasign.de/products/pdf-php-solutions/fpdi/</a><br>
  287. <br>
  288. You can then add some content to them.
  289. </li>
  290. <li id='q18'>
  291. <p><b>18.</b> <span class='question'>I'd like to make a search engine in PHP and index PDF files. Can I do it with FPDF?</span></p>
  292. No. But a GPL C utility does exist, pdftotext, which is able to extract the textual content from
  293. a PDF. It is provided with the Xpdf package:<br>
  294. <br>
  295. <a href="http://www.foolabs.com/xpdf/" target="_blank">http://www.foolabs.com/xpdf/</a>
  296. </li>
  297. <li id='q19'>
  298. <p><b>19.</b> <span class='question'>Can I convert an HTML page to PDF with FPDF?</span></p>
  299. Not real-world pages. But a GPL C utility does exist, htmldoc, which allows to do it and gives good results:<br>
  300. <br>
  301. <a href="http://www.htmldoc.org" target="_blank">http://www.htmldoc.org</a>
  302. </li>
  303. <li id='q20'>
  304. <p><b>20.</b> <span class='question'>Can I concatenate PDF files with FPDF?</span></p>
  305. Not directly, but it is possible to use <a href="http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/" target="_blank">FPDI</a>
  306. to perform this task. Some free command-line tools also exist:<br>
  307. <br>
  308. <a href="http://thierry.schmit.free.fr/spip/spip.php?article15&amp;lang=en" target="_blank">mbtPdfAsm</a><br>
  309. <a href="http://www.accesspdf.com/pdftk/" target="_blank">pdftk</a>
  310. </li>
  311. </ul>
  312. </body>
  313. </html>