| 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>
|
|---|
| 10 | Let's start with the classic example:
|
|---|
| 11 | <div class="source">
|
|---|
| 12 | <pre><code><?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">-></span>AddPage<span class="kw">();
|
|---|
| 17 | </span>$pdf<span class="kw">-></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">-></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">-></span>Output<span class="kw">();
|
|---|
| 20 | </span>?></code></pre>
|
|---|
| 21 | </div>
|
|---|
| 22 | <p class='demo'><a href='tuto1.php' target='_blank' class='demo'>[Demo]</a></p>
|
|---|
| 23 | After including the library file, we create an FPDF object.
|
|---|
| 24 | The <a href='../doc/fpdf.htm'>FPDF()</a> constructor is used here with the default values: pages are in A4 portrait and
|
|---|
| 25 | the 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>
|
|---|
| 30 | It's possible to use landscape (<code>L</code>), other page sizes (such as <code>Letter</code> and
|
|---|
| 31 | <code>Legal</code>) and units (<code>pt</code>, <code>cm</code>, <code>in</code>).
|
|---|
| 32 | <br>
|
|---|
| 33 | <br>
|
|---|
| 34 | There's no page at the moment, so we have to add one with <a href='../doc/addpage.htm'>AddPage()</a>. The origin
|
|---|
| 35 | is at the upper-left corner and the current position is by default set at 1 cm from the
|
|---|
| 36 | borders; the margins can be changed with <a href='../doc/setmargins.htm'>SetMargins()</a>.
|
|---|
| 37 | <br>
|
|---|
| 38 | <br>
|
|---|
| 39 | Before we can print text, it's mandatory to select a font with <a href='../doc/setfont.htm'>SetFont()</a>, otherwise the
|
|---|
| 40 | document would be invalid. We choose Arial bold 16:
|
|---|
| 41 | <div class="source">
|
|---|
| 42 | <pre><code>$pdf<span class="kw">-></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>
|
|---|
| 45 | We 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 user
|
|---|
| 47 | unit); it's the only exception. The other standard fonts are Times, Courier, Symbol and ZapfDingbats.
|
|---|
| 48 | <br>
|
|---|
| 49 | <br>
|
|---|
| 50 | We can now print a cell with <a href='../doc/cell.htm'>Cell()</a>. A cell is a rectangular area, possibly framed,
|
|---|
| 51 | which contains a line of text. It is output at the current position. We specify its dimensions,
|
|---|
| 52 | its text (centered or aligned), if borders should be drawn, and where the current position
|
|---|
| 53 | moves after it (to the right, below or to the beginning of the next line). To add a frame, we would do this:
|
|---|
| 54 | <div class="source">
|
|---|
| 55 | <pre><code>$pdf<span class="kw">-></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">);
|
|---|
| 56 | </span></code></pre>
|
|---|
| 57 | </div>
|
|---|
| 58 | To add a new cell next to it with centered text and go to the next line, we would do:
|
|---|
| 59 | <div class="source">
|
|---|
| 60 | <pre><code>$pdf<span class="kw">-></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">);
|
|---|
| 61 | </span></code></pre>
|
|---|
| 62 | </div>
|
|---|
| 63 | Remark: the line break can also be done with <a href='../doc/ln.htm'>Ln()</a>. This method additionnaly allows to specify
|
|---|
| 64 | the height of the break.
|
|---|
| 65 | <br>
|
|---|
| 66 | <br>
|
|---|
| 67 | Finally, the document is closed and sent to the browser with <a href='../doc/output.htm'>Output()</a>. We could have saved
|
|---|
| 68 | it to a file by passing the desired file name.
|
|---|
| 69 | <br>
|
|---|
| 70 | <br>
|
|---|
| 71 | <strong>Caution:</strong> in case when the PDF is sent to the browser, nothing else must be output by the
|
|---|
| 72 | script, neither before nor after (no HTML, not even a space or a carriage return). If you send something
|
|---|
| 73 | before, you will get the error message: "Some data has already been output, can't send PDF file". If you
|
|---|
| 74 | send something after, the document might not display.
|
|---|
| 75 | </body>
|
|---|
| 76 | </html>
|
|---|