source: trunk/client/inc/hpdf/_fpdf/tutorial/tuto5.htm@ 201

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

importo il progetto

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