forked from Setasign/FPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FAQ.htm
271 lines (243 loc) · 10.8 KB
/
FAQ.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>FAQ</title>
<link type="text/css" rel="stylesheet" href="fpdf.css">
<style type="text/css">
ul {list-style-type:none; margin:0; padding:0}
ul#answers li {margin-top:1.8em}
.question {font-weight:bold; color:#900000}
</style>
</head>
<body>
<h1>FAQ</h1>
<ul>
<li><b>1.</b> <a href='#q1'>What's exactly the license of FPDF? Are there any usage restrictions?</a></li>
<li><b>2.</b> <a href='#q2'>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>
<li><b>3.</b> <a href='#q3'>Accented letters are replaced with some strange characters like é.</a></li>
<li><b>4.</b> <a href='#q4'>I try to display the Euro symbol but it doesn't work.</a></li>
<li><b>5.</b> <a href='#q5'>I try to display a variable in the Header method but nothing prints.</a></li>
<li><b>6.</b> <a href='#q6'>I have defined the Header and Footer methods in my PDF class but nothing shows.</a></li>
<li><b>7.</b> <a href='#q7'>I can't make line breaks work. I put \n in the string printed by MultiCell but it doesn't work.</a></li>
<li><b>8.</b> <a href='#q8'>I use jQuery to generate the PDF but it doesn't show.</a></li>
<li><b>9.</b> <a href='#q9'>I draw a frame with very precise dimensions, but when printed I notice some differences.</a></li>
<li><b>10.</b> <a href='#q10'>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>
<li><b>11.</b> <a href='#q11'>How can I put a background in my PDF?</a></li>
<li><b>12.</b> <a href='#q12'>How can I set a specific header or footer on the first page?</a></li>
<li><b>13.</b> <a href='#q13'>I'd like to use extensions provided by different scripts. How can I combine them?</a></li>
<li><b>14.</b> <a href='#q14'>How can I open the PDF in a new tab?</a></li>
<li><b>15.</b> <a href='#q15'>How can I send the PDF by email?</a></li>
<li><b>16.</b> <a href='#q16'>What's the limit of the file sizes I can generate with FPDF?</a></li>
<li><b>17.</b> <a href='#q17'>Can I modify a PDF with FPDF?</a></li>
<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>
<li><b>19.</b> <a href='#q19'>Can I convert an HTML page to PDF with FPDF?</a></li>
<li><b>20.</b> <a href='#q20'>Can I concatenate PDF files with FPDF?</a></li>
</ul>
<ul id='answers'>
<li id='q1'>
<p><b>1.</b> <span class='question'>What's exactly the license of FPDF? Are there any usage restrictions?</span></p>
FPDF is released under a permissive license: there is no usage restriction. You may embed it
freely in your application (commercial or not), with or without modifications.
</li>
<li id='q2'>
<p><b>2.</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>
You must send nothing to the browser except the PDF itself: no HTML, no space, no carriage return. A common
case is having extra blank at the end of an included script file.<br>
If you can't figure out where the problem comes from, this other message appearing just before will help you:<br>
<br>
<b>Warning:</b> Cannot modify header information - headers already sent by (output started at script.php:X)<br>
<br>
It means that script.php outputs something at line X. Go to that line and fix it.
In case the message doesn't show, check if PHP warnings are enabled. If they are not, enable them. If they are,
try adding this at the very beginning of your script:
<div class="doc-source">
<pre><code>ob_end_clean();</code></pre>
</div>
</li>
<li id='q3'>
<p><b>3.</b> <span class='question'>Accented letters are replaced with some strange characters like é.</span></p>
Don't use UTF-8 with the standard fonts; they expect text encoded in ISO-8859-1 or windows-1252.
You can use utf8_decode() to perform a conversion to ISO-8859-1:
<div class="doc-source">
<pre><code>$str = utf8_decode($str);</code></pre>
</div>
But some characters such as Euro won't be translated correctly. If the iconv extension is available, the
right way to do it is the following:
<div class="doc-source">
<pre><code>$str = iconv('UTF-8', 'windows-1252', $str);</code></pre>
</div>
In case you need characters outside windows-1252, take a look at tutorial #7 or
<a href="http://www.fpdf.org/?go=script&id=92" target="_blank">tFPDF</a>.
</li>
<li id='q4'>
<p><b>4.</b> <span class='question'>I try to display the Euro symbol but it doesn't work.</span></p>
The standard fonts have the Euro character at position 128. You can define a constant like this
for convenience:
<div class="doc-source">
<pre><code>define('EURO', chr(128));</code></pre>
</div>
</li>
<li id='q5'>
<p><b>5.</b> <span class='question'>I try to display a variable in the Header method but nothing prints.</span></p>
You have to use the <code>global</code> keyword to access global variables, for example:
<div class="doc-source">
<pre><code>function Header()
{
global $title;
$this->SetFont('Arial', 'B', 15);
$this->Cell(0, 10, $title, 1, 1, 'C');
}
$title = 'My title';</code></pre>
</div>
Alternatively, you can use an object property:
<div class="doc-source">
<pre><code>function Header()
{
$this->SetFont('Arial', 'B', 15);
$this->Cell(0, 10, $this->title, 1, 1, 'C');
}
$pdf->title = 'My title';</code></pre>
</div>
</li>
<li id='q6'>
<p><b>6.</b> <span class='question'>I have defined the Header and Footer methods in my PDF class but nothing shows.</span></p>
You have to create an object from the PDF class, not FPDF:
<div class="doc-source">
<pre><code>$pdf = new PDF();</code></pre>
</div>
</li>
<li id='q7'>
<p><b>7.</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>
You have to enclose your string with double quotes, not single ones.
</li>
<li id='q8'>
<p><b>8.</b> <span class='question'>I use jQuery to generate the PDF but it doesn't show.</span></p>
Don't use an AJAX request to retrieve the PDF.
</li>
<li id='q9'>
<p><b>9.</b> <span class='question'>I draw a frame with very precise dimensions, but when printed I notice some differences.</span></p>
To respect dimensions, select "None" for the Page Scaling setting instead of "Shrink to Printable Area" in the print dialog box.
</li>
<li id='q10'>
<p><b>10.</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>
Printers have physical margins (different depending on the models); it is therefore impossible to remove
them and print on the whole surface of the paper.
</li>
<li id='q11'>
<p><b>11.</b> <span class='question'>How can I put a background in my PDF?</span></p>
For a picture, call Image() in the Header() method, before any other output. To set a background color, use Rect().
</li>
<li id='q12'>
<p><b>12.</b> <span class='question'>How can I set a specific header or footer on the first page?</span></p>
Just test the page number:
<div class="doc-source">
<pre><code>function Header()
{
if($this->PageNo()==1)
{
//First page
...
}
else
{
//Other pages
...
}
}</code></pre>
</div>
</li>
<li id='q13'>
<p><b>13.</b> <span class='question'>I'd like to use extensions provided by different scripts. How can I combine them?</span></p>
Use an inheritance chain. If you have two classes, say A in a.php:
<div class="doc-source">
<pre><code>require('fpdf.php');
class A extends FPDF
{
...
}</code></pre>
</div>
and B in b.php:
<div class="doc-source">
<pre><code>require('fpdf.php');
class B extends FPDF
{
...
}</code></pre>
</div>
then make B extend A:
<div class="doc-source">
<pre><code>require('a.php');
class B extends A
{
...
}</code></pre>
</div>
and make your own class extend B:
<div class="doc-source">
<pre><code>require('b.php');
class PDF extends B
{
...
}
$pdf = new PDF();</code></pre>
</div>
</li>
<li id='q14'>
<p><b>14.</b> <span class='question'>How can I open the PDF in a new tab?</span></p>
Just do the same as you would for an HTML page or anything else: add a target="_blank" to your link or form.
</li>
<li id='q15'>
<p><b>15.</b> <span class='question'>How can I send the PDF by email?</span></p>
As for any other file, but an easy way is to use <a href="https://github.com/PHPMailer/PHPMailer" target="_blank">PHPMailer</a> and
its in-memory attachment:
<div class="doc-source">
<pre><code>$mail = new PHPMailer();
...
$doc = $pdf->Output('S');
$mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf');
$mail->Send();</code></pre>
</div>
</li>
<li id='q16'>
<p><b>16.</b> <span class='question'>What's the limit of the file sizes I can generate with FPDF?</span></p>
There is no particular limit. There are some constraints, however:
<br>
<br>
- There is usually a maximum memory size allocated to PHP scripts. For very big documents,
especially with images, the limit may be reached (the file being built in memory). The
parameter is configured in the php.ini file.
<br>
<br>
- The maximum execution time allocated to scripts defaults to 30 seconds. This limit can of course
be easily reached. It is configured in php.ini and may be altered dynamically with set_time_limit().
<br>
<br>
You can work around the memory limit with <a href="http://www.fpdf.org/?go=script&id=76" target="_blank">this script</a>.
</li>
<li id='q17'>
<p><b>17.</b> <span class='question'>Can I modify a PDF with FPDF?</span></p>
It's possible to import pages from an existing PDF document thanks to the
<a href="https://www.setasign.com/products/fpdi/about/" target="_blank">FPDI</a> extension.
Then you can add some content to them.
</li>
<li id='q18'>
<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>
No. But a GPL C utility does exist, pdftotext, which is able to extract the textual content from a PDF.
It's provided with the <a href="https://www.xpdfreader.com" target="_blank">Xpdf</a> package.
</li>
<li id='q19'>
<p><b>19.</b> <span class='question'>Can I convert an HTML page to PDF with FPDF?</span></p>
Not real-world pages. But a GPL C utility does exist, <a href="https://www.msweet.org/htmldoc/" target="_blank">HTMLDOC</a>,
which allows to do it and gives good results.
</li>
<li id='q20'>
<p><b>20.</b> <span class='question'>Can I concatenate PDF files with FPDF?</span></p>
Not directly, but it's possible to use <a href="https://www.setasign.com/products/fpdi/demos/concatenate-fake/" target="_blank">FPDI</a>
to perform that task. Some free command-line tools also exist:
<a href="https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/" target="_blank">pdftk</a> and
<a href="http://thierry.schmit.free.fr/spip/spip.php?article15" target="_blank">mbtPdfAsm</a>.
</li>
</ul>
</body>
</html>