tuto5.htm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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>Tables</title>
  6. <link type="text/css" rel="stylesheet" href="../fpdf.css">
  7. </head>
  8. <body>
  9. <h1>Tables</h1>
  10. This tutorial shows different ways to make tables.
  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">// Load data
  17. </span><span class="kw">function </span>LoadData<span class="kw">(</span>$file<span class="kw">)
  18. {
  19. </span><span class="cmt">// Read file lines
  20. </span>$lines <span class="kw">= </span>file<span class="kw">(</span>$file<span class="kw">);
  21. </span>$data <span class="kw">= array();
  22. foreach(</span>$lines <span class="kw">as </span>$line<span class="kw">)
  23. </span>$data<span class="kw">[] = </span>explode<span class="kw">(</span><span class="str">';'</span><span class="kw">,</span>trim<span class="kw">(</span>$line<span class="kw">));
  24. return </span>$data<span class="kw">;
  25. }
  26. </span><span class="cmt">// Simple table
  27. </span><span class="kw">function </span>BasicTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
  28. {
  29. </span><span class="cmt">// Header
  30. </span><span class="kw">foreach(</span>$header <span class="kw">as </span>$col<span class="kw">)
  31. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>40<span class="kw">,</span>7<span class="kw">,</span>$col<span class="kw">,</span>1<span class="kw">);
  32. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  33. </span><span class="cmt">// Data
  34. </span><span class="kw">foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
  35. {
  36. foreach(</span>$row <span class="kw">as </span>$col<span class="kw">)
  37. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>40<span class="kw">,</span>6<span class="kw">,</span>$col<span class="kw">,</span>1<span class="kw">);
  38. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  39. }
  40. }
  41. </span><span class="cmt">// Better table
  42. </span><span class="kw">function </span>ImprovedTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
  43. {
  44. </span><span class="cmt">// Column widths
  45. </span>$w <span class="kw">= array(</span>40<span class="kw">, </span>35<span class="kw">, </span>40<span class="kw">, </span>45<span class="kw">);
  46. </span><span class="cmt">// Header
  47. </span><span class="kw">for(</span>$i<span class="kw">=</span>0<span class="kw">;</span>$i<span class="kw">&lt;</span>count<span class="kw">(</span>$header<span class="kw">);</span>$i<span class="kw">++)
  48. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>$i<span class="kw">],</span>7<span class="kw">,</span>$header<span class="kw">[</span>$i<span class="kw">],</span>1<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
  49. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  50. </span><span class="cmt">// Data
  51. </span><span class="kw">foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
  52. {
  53. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>0<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>0<span class="kw">],</span><span class="str">'LR'</span><span class="kw">);
  54. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>1<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>1<span class="kw">],</span><span class="str">'LR'</span><span class="kw">);
  55. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>2<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>2<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">);
  56. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>3<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>3<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">);
  57. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  58. }
  59. </span><span class="cmt">// Closing line
  60. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>array_sum<span class="kw">(</span>$w<span class="kw">),</span>0<span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'T'</span><span class="kw">);
  61. }
  62. </span><span class="cmt">// Colored table
  63. </span><span class="kw">function </span>FancyTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
  64. {
  65. </span><span class="cmt">// Colors, line width and bold font
  66. </span>$<span class="kw">this-&gt;</span>SetFillColor<span class="kw">(</span>255<span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">);
  67. </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>255<span class="kw">);
  68. </span>$<span class="kw">this-&gt;</span>SetDrawColor<span class="kw">(</span>128<span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">);
  69. </span>$<span class="kw">this-&gt;</span>SetLineWidth<span class="kw">(</span>.3<span class="kw">);
  70. </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">);
  71. </span><span class="cmt">// Header
  72. </span>$w <span class="kw">= array(</span>40<span class="kw">, </span>35<span class="kw">, </span>40<span class="kw">, </span>45<span class="kw">);
  73. for(</span>$i<span class="kw">=</span>0<span class="kw">;</span>$i<span class="kw">&lt;</span>count<span class="kw">(</span>$header<span class="kw">);</span>$i<span class="kw">++)
  74. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>$i<span class="kw">],</span>7<span class="kw">,</span>$header<span class="kw">[</span>$i<span class="kw">],</span>1<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">,</span>true<span class="kw">);
  75. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  76. </span><span class="cmt">// Color and font restoration
  77. </span>$<span class="kw">this-&gt;</span>SetFillColor<span class="kw">(</span>224<span class="kw">,</span>235<span class="kw">,</span>255<span class="kw">);
  78. </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>0<span class="kw">);
  79. </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">);
  80. </span><span class="cmt">// Data
  81. </span>$fill <span class="kw">= </span>false<span class="kw">;
  82. foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
  83. {
  84. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>0<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>0<span class="kw">],</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'L'</span><span class="kw">,</span>$fill<span class="kw">);
  85. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>1<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>1<span class="kw">],</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'L'</span><span class="kw">,</span>$fill<span class="kw">);
  86. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>2<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>2<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">,</span>$fill<span class="kw">);
  87. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">[</span>3<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>3<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">,</span>$fill<span class="kw">);
  88. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  89. </span>$fill <span class="kw">= !</span>$fill<span class="kw">;
  90. }
  91. </span><span class="cmt">// Closing line
  92. </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>array_sum<span class="kw">(</span>$w<span class="kw">),</span>0<span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'T'</span><span class="kw">);
  93. }
  94. }
  95. </span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
  96. </span><span class="cmt">// Column headings
  97. </span>$header <span class="kw">= array(</span><span class="str">'Country'</span><span class="kw">, </span><span class="str">'Capital'</span><span class="kw">, </span><span class="str">'Area (sq km)'</span><span class="kw">, </span><span class="str">'Pop. (thousands)'</span><span class="kw">);
  98. </span><span class="cmt">// Data loading
  99. </span>$data <span class="kw">= </span>$pdf<span class="kw">-&gt;</span>LoadData<span class="kw">(</span><span class="str">'countries.txt'</span><span class="kw">);
  100. </span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>14<span class="kw">);
  101. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  102. </span>$pdf<span class="kw">-&gt;</span>BasicTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
  103. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  104. </span>$pdf<span class="kw">-&gt;</span>ImprovedTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
  105. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  106. </span>$pdf<span class="kw">-&gt;</span>FancyTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
  107. </span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
  108. </span>?&gt;</code></pre>
  109. </div>
  110. <p class='demo'><a href='tuto5.php' target='_blank' class='demo'>[Demo]</a></p>
  111. A table being just a collection of cells, it's natural to build one from them. The first
  112. example is achieved in the most basic way possible: simple framed cells, all of the same size
  113. and left aligned. The result is rudimentary but very quick to obtain.
  114. <br>
  115. <br>
  116. The second table brings some improvements: each column has its own width, headings are centered,
  117. and numbers right aligned. Moreover, horizontal lines have been removed. This is done by means
  118. of the <code>border</code> parameter of the <a href='../doc/cell.htm'>Cell()</a> method, which specifies which sides of the
  119. cell must be drawn. Here we want the left (<code>L</code>) and right (<code>R</code>) ones. It remains
  120. the problem of the horizontal line to finish the table. There are two possibilities: either
  121. check for the last line in the loop, in which case we use <code>LRB</code> for the <code>border</code>
  122. parameter; or, as done here, add the line once the loop is over.
  123. <br>
  124. <br>
  125. The third table is similar to the second one but uses colors. Fill, text and line colors are
  126. simply specified. Alternate coloring for rows is obtained by using alternatively transparent
  127. and filled cells.
  128. </body>
  129. </html>