tuto1.htm 4.7 KB

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