tuto5.htm
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tablas</title>
<link type="text/css" rel="stylesheet" href="../fpdf.css">
</head>
<body>
<h1>Tablas</h1>
Este tutorial se explicar como crear tablas fcilmente.
<div class="source">
<pre><code><?php
<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
class </span>PDF <span class="kw">extends </span>FPDF
<span class="kw">{
</span><span class="cmt">// Cargar los datos
</span><span class="kw">function </span>LoadData<span class="kw">(</span>$file<span class="kw">)
{
</span><span class="cmt">// Leer las lneas del fichero
</span>$lines <span class="kw">= </span>file<span class="kw">(</span>$file<span class="kw">);
</span>$data <span class="kw">= array();
foreach(</span>$lines <span class="kw">as </span>$line<span class="kw">)
</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">));
return </span>$data<span class="kw">;
}
</span><span class="cmt">// Tabla simple
</span><span class="kw">function </span>BasicTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
{
</span><span class="cmt">// Cabecera
</span><span class="kw">foreach(</span>$header <span class="kw">as </span>$col<span class="kw">)
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Datos
</span><span class="kw">foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
{
foreach(</span>$row <span class="kw">as </span>$col<span class="kw">)
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
}
}
</span><span class="cmt">// Una tabla ms completa
</span><span class="kw">function </span>ImprovedTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
{
</span><span class="cmt">// Anchuras de las columnas
</span>$w <span class="kw">= array(</span>40<span class="kw">, </span>35<span class="kw">, </span>45<span class="kw">, </span>40<span class="kw">);
</span><span class="cmt">// Cabeceras
</span><span class="kw">for(</span>$i<span class="kw">=</span>0<span class="kw">;</span>$i<span class="kw"><</span>count<span class="kw">(</span>$header<span class="kw">);</span>$i<span class="kw">++)
</span>$<span class="kw">this-></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>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Datos
</span><span class="kw">foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
{
</span>$<span class="kw">this-></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>$<span class="kw">this-></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>$<span class="kw">this-></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>$<span class="kw">this-></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>$<span class="kw">this-></span>Ln<span class="kw">();
}
</span><span class="cmt">// Lnea de cierre
</span>$<span class="kw">this-></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">);
}
</span><span class="cmt">// Tabla coloreada
</span><span class="kw">function </span>FancyTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
{
</span><span class="cmt">// Colores, ancho de lnea y fuente en negrita
</span>$<span class="kw">this-></span>SetFillColor<span class="kw">(</span>255<span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetTextColor<span class="kw">(</span>255<span class="kw">);
</span>$<span class="kw">this-></span>SetDrawColor<span class="kw">(</span>128<span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetLineWidth<span class="kw">(</span>.3<span class="kw">);
</span>$<span class="kw">this-></span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">);
</span><span class="cmt">// Cabecera
</span>$w <span class="kw">= array(</span>40<span class="kw">, </span>35<span class="kw">, </span>45<span class="kw">, </span>40<span class="kw">);
for(</span>$i<span class="kw">=</span>0<span class="kw">;</span>$i<span class="kw"><</span>count<span class="kw">(</span>$header<span class="kw">);</span>$i<span class="kw">++)
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Restauracin de colores y fuentes
</span>$<span class="kw">this-></span>SetFillColor<span class="kw">(</span>224<span class="kw">,</span>235<span class="kw">,</span>255<span class="kw">);
</span>$<span class="kw">this-></span>SetTextColor<span class="kw">(</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">);
</span><span class="cmt">// Datos
</span>$fill <span class="kw">= </span>false<span class="kw">;
foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
{
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span>$fill <span class="kw">= !</span>$fill<span class="kw">;
}
</span><span class="cmt">// Lnea de cierre
</span>$<span class="kw">this-></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">);
}
}
</span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
</span><span class="cmt">// Ttulos de las columnas
</span>$header <span class="kw">= array(</span><span class="str">'Pas'</span><span class="kw">, </span><span class="str">'Capital'</span><span class="kw">, </span><span class="str">'Superficie (km2)'</span><span class="kw">, </span><span class="str">'Pobl. (en miles)'</span><span class="kw">);
</span><span class="cmt">// Carga de datos
</span>$data <span class="kw">= </span>$pdf<span class="kw">-></span>LoadData<span class="kw">(</span><span class="str">'paises.txt'</span><span class="kw">);
</span>$pdf<span class="kw">-></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">);
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
</span>$pdf<span class="kw">-></span>BasicTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
</span>$pdf<span class="kw">-></span>ImprovedTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
</span>$pdf<span class="kw">-></span>FancyTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
</span>$pdf<span class="kw">-></span>Output<span class="kw">();
</span>?></code></pre>
</div>
<p class='demo'><a href='tuto5.php' target='_blank' class='demo'>[Demo]</a></p>
Siendo una tabla un conjunto de celdas, lo natural es construirla de ellas. El
primer ejemplo es el ms bsico posible: celdas con bordes simples, todas del mismo
tamao y alineadas a la izquierda. El resultado es algo rudimentario, pero es
muy rpido de conseguir.
<br>
<br>
La segunda tabla tiene algunas mejoras: cada columna tiene su propio ancho, los ttulos
estn centrados y el texto se alinea a la derecha. Ms an, las lneas horizontales se
han eliminado. Esto se consigue mediante el parmetro <code>border</code>
del mtodo <a href='../doc/cell.htm'>Cell()</a>, que especifica qu bordes de la celda deben imprimirse.
En este caso, queremos que sean los de la izquierda (<code>L</code>) y los de la derecha
(<code>R</code>). Seguimos teniendo el problema de la lnea horizontal de fin de tabla.
Hay dos posibilidades: o comprobar si estamos en la ltima lnea en el bucle, en cuyo caso
usaremos <code>LRB</code> para el parmtro <code>border</code>; o, como hemos hecho aqu,
aadir la lnea una vez que el bucle ha terminado.
<br>
<br>
La tercera tabla es similar a la segunda, salvo por el uso de colores. Simplemente hemos
especificado los colores de relleno, texto y lnea. El coloreado alternativo de las filas
se consigue alternando celdas transparentes y coloreadas.
</body>
</html>