source: branches/version-2_11-dev/data/module/fpdf/FAQ.htm @ 20993

Revision 20993, 14.3 KB checked in by Seasoft, 13 years ago (diff)

#1374 (依存ライブラリのアップデート)

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