source: trunk/client/inc/hpdf/_fpdf/tutorial/tuto1.htm@ 64

Last change on this file since 64 was 2, checked in by root, 15 years ago

importo il progetto

File size: 4.7 KB
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>Minimal example</title>
6<link type="text/css" rel="stylesheet" href="../fpdf.css">
7</head>
8<body>
9<h1>Minimal example</h1>
10Let's start with the classic example:
11<div class="source">
12<pre><code>&lt;?php
13<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
14
15</span>$pdf<span class="kw">=new </span>FPDF<span class="kw">();
16</span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
17</span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">,</span>16<span class="kw">);
18</span>$pdf<span class="kw">-&gt;</span>Cell<span class="kw">(</span>40<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Hello World!'</span><span class="kw">);
19</span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
20</span>?&gt;</code></pre>
21</div>
22<p class='demo'><a href='tuto1.php' target='_blank' class='demo'>[Demo]</a></p>
23After including the library file, we create an FPDF object.
24The <a href='../doc/fpdf.htm'>FPDF()</a> constructor is used here with the default values: pages are in A4 portrait and
25the unit of measure is millimeter. It could have been specified explicitly with:
26<div class="source">
27<pre><code>$pdf<span class="kw">=new </span>FPDF<span class="kw">(</span><span class="str">'P'</span><span class="kw">,</span><span class="str">'mm'</span><span class="kw">,</span><span class="str">'A4'</span><span class="kw">);
28</span></code></pre>
29</div>
30It is possible to use landscape (<code>L</code>), other page formats (such as <code>Letter</code> and
31<code>Legal</code>) and units of measure (<code>pt</code>, <code>cm</code>, <code>in</code>).
32<br>
33<br>
34There is no page for the moment, so we have to add one with <a href='../doc/addpage.htm'>AddPage()</a>. The origin
35is at the upper-left corner and the current position is by default placed at 1 cm from the
36borders; the margins can be changed with <a href='../doc/setmargins.htm'>SetMargins()</a>.
37<br>
38<br>
39Before we can print text, it is mandatory to select a font with <a href='../doc/setfont.htm'>SetFont()</a>, otherwise the
40document would be invalid. We choose Arial bold 16:
41<div class="source">
42<pre><code>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">,</span>16<span class="kw">);
43</span></code></pre>
44</div>
45We could have specified italics with I, underlined with U or a regular font with an empty string
46(or any combination). Note that the font size is given in points, not millimeters (or another
47user unit); it is the only exception. The other standard fonts are Times, Courier, Symbol and
48ZapfDingbats.
49<br>
50<br>
51We can now print a cell with <a href='../doc/cell.htm'>Cell()</a>. A cell is a rectangular area, possibly framed,
52which contains a line of text. It is output at the current position. We specify its dimensions,
53its text (centered or aligned), if borders should be drawn, and where the current position
54moves after it (to the right, below or to the beginning of the next line). To add a frame, we would do this:
55<div class="source">
56<pre><code>$pdf<span class="kw">-&gt;</span>Cell<span class="kw">(</span>40<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Hello World !'</span><span class="kw">,</span>1<span class="kw">);
57</span></code></pre>
58</div>
59To add a new cell next to it with centered text and go to the next line, we would do:
60<div class="source">
61<pre><code>$pdf<span class="kw">-&gt;</span>Cell<span class="kw">(</span>60<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Powered by FPDF.'</span><span class="kw">,</span>0<span class="kw">,</span>1<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
62</span></code></pre>
63</div>
64Remark: the line break can also be done with <a href='../doc/ln.htm'>Ln()</a>. This method additionnaly allows to specify
65the height of the break.
66<br>
67<br>
68Finally, the document is closed and sent to the browser with <a href='../doc/output.htm'>Output()</a>. We could have saved
69it to a file by passing the desired file name.
70<br>
71<br>
72<strong>Caution:</strong> in case when the PDF is sent to the browser, nothing else must be output by the
73script, neither before nor after (no HTML, not even a space or a carriage return). If you send something
74before, you will get the error message: "Some data has already been output, can't send PDF file". If you
75send something after, the document might not display.
76</body>
77</html>
Note: See TracBrowser for help on using the repository browser.