tuto2.htm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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>Header, footer, page break and image</title>
  6. <link type="text/css" rel="stylesheet" href="../fpdf.css">
  7. </head>
  8. <body>
  9. <h1>Header, footer, page break and image</h1>
  10. Here's a two page example with header, footer and logo:
  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. class </span>PDF <span class="kw">extends </span>FPDF
  15. <span class="kw">{
  16. </span><span class="cmt">// Page header
  17. </span><span class="kw">function </span>Header<span class="kw">()
  18. {
  19. </span><span class="cmt">// Logo
  20. </span>$<span class="kw">this-&gt;</span>Image<span class="kw">(</span><span class="str">'logo.png'</span><span class="kw">,</span>10<span class="kw">,</span>6<span class="kw">,</span>30<span class="kw">);
  21. </span><span class="cmt">// Arial bold 15
  22. </span>$<span class="kw">this-&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>15<span class="kw">);
  23. </span><span class="cmt">// Move to the right
  24. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>80<span class="kw">);
  25. </span><span class="cmt">// Title
  26. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>30<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Title'</span><span class="kw">,</span>1<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
  27. </span><span class="cmt">// Line break
  28. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>20<span class="kw">);
  29. }
  30. </span><span class="cmt">// Page footer
  31. </span><span class="kw">function </span>Footer<span class="kw">()
  32. {
  33. </span><span class="cmt">// Position at 1.5 cm from bottom
  34. </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(-</span>15<span class="kw">);
  35. </span><span class="cmt">// Arial italic 8
  36. </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">'I'</span><span class="kw">,</span>8<span class="kw">);
  37. </span><span class="cmt">// Page number
  38. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Page '</span><span class="kw">.</span>$<span class="kw">this-&gt;</span>PageNo<span class="kw">().</span><span class="str">'/{nb}'</span><span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
  39. }
  40. }
  41. </span><span class="cmt">// Instanciation of inherited class
  42. </span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
  43. </span>$pdf<span class="kw">-&gt;</span>AliasNbPages<span class="kw">();
  44. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  45. </span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Times'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>12<span class="kw">);
  46. for(</span>$i<span class="kw">=</span>1<span class="kw">;</span>$i<span class="kw">&lt;=</span>40<span class="kw">;</span>$i<span class="kw">++)
  47. </span>$pdf<span class="kw">-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Printing line number '</span><span class="kw">.</span>$i<span class="kw">,</span>0<span class="kw">,</span>1<span class="kw">);
  48. </span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
  49. </span>?&gt;</code></pre>
  50. </div>
  51. <p class='demo'><a href='tuto2.php' target='_blank' class='demo'>[Demo]</a></p>
  52. This example makes use of the <a href='../doc/header.htm'>Header()</a> and <a href='../doc/footer.htm'>Footer()</a> methods to process page headers and
  53. footers. They are called automatically. They already exist in the FPDF class but do nothing,
  54. therefore we have to extend the class and override them.
  55. <br>
  56. <br>
  57. The logo is printed with the <a href='../doc/image.htm'>Image()</a> method by specifying its upper-left corner and
  58. its width. The height is calculated automatically to respect the image proportions.
  59. <br>
  60. <br>
  61. To print the page number, a null value is passed as the cell width. It means that the cell
  62. should extend up to the right margin of the page; this is handy to center text. The current page
  63. number is returned by the <a href='../doc/pageno.htm'>PageNo()</a> method; as for the total number of pages, it's obtained
  64. via the special value <code>{nb}</code> which is substituted when the document is finished
  65. (provided you first called <a href='../doc/aliasnbpages.htm'>AliasNbPages()</a>).
  66. <br>
  67. Note the use of the <a href='../doc/sety.htm'>SetY()</a> method which allows to set position at an absolute location in
  68. the page, starting from the top or the bottom.
  69. <br>
  70. <br>
  71. Another interesting feature is used here: the automatic page breaking. As soon as a cell would
  72. cross a limit in the page (at 2 centimeters from the bottom by default), a break is issued
  73. and the font restored. Although the header and footer select their own font (Arial), the body
  74. continues with Times. This mechanism of automatic restoration also applies to colors and line
  75. width. The limit which triggers page breaks can be set with <a href='../doc/setautopagebreak.htm'>SetAutoPageBreak()</a>.
  76. </body>
  77. </html>