tuto4.htm
9.61 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
<!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>Columnas mltiples</title>
<link type="text/css" rel="stylesheet" href="../fpdf.css">
</head>
<body>
<h1>Columnas mltiples</h1>
Este ejemplo es una variante del anterior en el que se mostrar como disponer texto
en varias columnas.
<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">{
protected </span>$col <span class="kw">= </span>0<span class="kw">; </span><span class="cmt">// Columna actual
</span><span class="kw">protected </span>$y0<span class="kw">; </span><span class="cmt">// Ordenada de comienzo de la columna
</span><span class="kw">function </span>Header<span class="kw">()
{
</span><span class="cmt">// Cabacera
</span><span class="kw">global </span>$title<span class="kw">;
</span>$<span class="kw">this-></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">);
</span>$w <span class="kw">= </span>$<span class="kw">this-></span>GetStringWidth<span class="kw">(</span>$title<span class="kw">)+</span>6<span class="kw">;
</span>$<span class="kw">this-></span>SetX<span class="kw">((</span>210<span class="kw">-</span>$w<span class="kw">)/</span>2<span class="kw">);
</span>$<span class="kw">this-></span>SetDrawColor<span class="kw">(</span>0<span class="kw">,</span>80<span class="kw">,</span>180<span class="kw">);
</span>$<span class="kw">this-></span>SetFillColor<span class="kw">(</span>230<span class="kw">,</span>230<span class="kw">,</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetTextColor<span class="kw">(</span>220<span class="kw">,</span>50<span class="kw">,</span>50<span class="kw">);
</span>$<span class="kw">this-></span>SetLineWidth<span class="kw">(</span>1<span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">,</span>9<span class="kw">,</span>$title<span class="kw">,</span>1<span class="kw">,</span>1<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>10<span class="kw">);
</span><span class="cmt">// Guardar ordenada
</span>$<span class="kw">this-></span>y0 <span class="kw">= </span>$<span class="kw">this-></span>GetY<span class="kw">();
}
function </span>Footer<span class="kw">()
{
</span><span class="cmt">// Pie de pgina
</span>$<span class="kw">this-></span>SetY<span class="kw">(-</span>15<span class="kw">);
</span>$<span class="kw">this-></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">);
</span>$<span class="kw">this-></span>SetTextColor<span class="kw">(</span>128<span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>0<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Pgina '</span><span class="kw">.</span>$<span class="kw">this-></span>PageNo<span class="kw">(),</span>0<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
}
function </span>SetCol<span class="kw">(</span>$col<span class="kw">)
{
</span><span class="cmt">// Establecer la posicin de una columna dada
</span>$<span class="kw">this-></span>col <span class="kw">= </span>$col<span class="kw">;
</span>$x <span class="kw">= </span>10<span class="kw">+</span>$col<span class="kw">*</span>65<span class="kw">;
</span>$<span class="kw">this-></span>SetLeftMargin<span class="kw">(</span>$x<span class="kw">);
</span>$<span class="kw">this-></span>SetX<span class="kw">(</span>$x<span class="kw">);
}
function </span>AcceptPageBreak<span class="kw">()
{
</span><span class="cmt">// Mtodo que acepta o no el salto automtico de pgina
</span><span class="kw">if(</span>$<span class="kw">this-></span>col<span class="kw"><</span>2<span class="kw">)
{
</span><span class="cmt">// Ir a la siguiente columna
</span>$<span class="kw">this-></span>SetCol<span class="kw">(</span>$<span class="kw">this-></span>col<span class="kw">+</span>1<span class="kw">);
</span><span class="cmt">// Establecer la ordenada al principio
</span>$<span class="kw">this-></span>SetY<span class="kw">(</span>$<span class="kw">this-></span>y0<span class="kw">);
</span><span class="cmt">// Seguir en esta pgina
</span><span class="kw">return </span>false<span class="kw">;
}
else
{
</span><span class="cmt">// Volver a la primera columna
</span>$<span class="kw">this-></span>SetCol<span class="kw">(</span>0<span class="kw">);
</span><span class="cmt">// Salto de pgina
</span><span class="kw">return </span>true<span class="kw">;
}
}
function </span>ChapterTitle<span class="kw">(</span>$num<span class="kw">, </span>$label<span class="kw">)
{
</span><span class="cmt">// Ttulo
</span>$<span class="kw">this-></span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>12<span class="kw">);
</span>$<span class="kw">this-></span>SetFillColor<span class="kw">(</span>200<span class="kw">,</span>220<span class="kw">,</span>255<span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>0<span class="kw">,</span>6<span class="kw">,</span><span class="str">"Captulo </span>$num<span class="str"> : </span>$label<span class="str">"</span><span class="kw">,</span>0<span class="kw">,</span>1<span class="kw">,</span><span class="str">'L'</span><span class="kw">,</span>true<span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">(</span>4<span class="kw">);
</span><span class="cmt">// Guardar ordenada
</span>$<span class="kw">this-></span>y0 <span class="kw">= </span>$<span class="kw">this-></span>GetY<span class="kw">();
}
function </span>ChapterBody<span class="kw">(</span>$file<span class="kw">)
{
</span><span class="cmt">// Abrir fichero de texto
</span>$txt <span class="kw">= </span>file_get_contents<span class="kw">(</span>$file<span class="kw">);
</span><span class="cmt">// Fuente
</span>$<span class="kw">this-></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">);
</span><span class="cmt">// Imprimir texto en una columna de 6 cm de ancho
</span>$<span class="kw">this-></span>MultiCell<span class="kw">(</span>60<span class="kw">,</span>5<span class="kw">,</span>$txt<span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Cita en itlica
</span>$<span class="kw">this-></span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'I'</span><span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>0<span class="kw">,</span>5<span class="kw">,</span><span class="str">'(fin del extracto)'</span><span class="kw">);
</span><span class="cmt">// Volver a la primera columna
</span>$<span class="kw">this-></span>SetCol<span class="kw">(</span>0<span class="kw">);
}
function </span>PrintChapter<span class="kw">(</span>$num<span class="kw">, </span>$title<span class="kw">, </span>$file<span class="kw">)
{
</span><span class="cmt">// Aadir captulo
</span>$<span class="kw">this-></span>AddPage<span class="kw">();
</span>$<span class="kw">this-></span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">);
</span>$<span class="kw">this-></span>ChapterBody<span class="kw">(</span>$file<span class="kw">);
}
}
</span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
</span>$title <span class="kw">= </span><span class="str">'20000 Leguas de Viaje Submarino'</span><span class="kw">;
</span>$pdf<span class="kw">-></span>SetTitle<span class="kw">(</span>$title<span class="kw">);
</span>$pdf<span class="kw">-></span>SetAuthor<span class="kw">(</span><span class="str">'Julio Verne'</span><span class="kw">);
</span>$pdf<span class="kw">-></span>PrintChapter<span class="kw">(</span>1<span class="kw">,</span><span class="str">'UN RIZO DE HUIDA'</span><span class="kw">,</span><span class="str">'20k_c1.txt'</span><span class="kw">);
</span>$pdf<span class="kw">-></span>PrintChapter<span class="kw">(</span>2<span class="kw">,</span><span class="str">'LOS PROS Y LOS CONTRAS'</span><span class="kw">,</span><span class="str">'20k_c2.txt'</span><span class="kw">);
</span>$pdf<span class="kw">-></span>Output<span class="kw">();
</span>?></code></pre>
</div>
<p class='demo'><a href='tuto4.php' target='_blank' class='demo'>[Demo]</a></p>
El mtodo clave usado es <a href='../doc/acceptpagebreak.htm'>AcceptPageBreak()</a>. Permite aceptar o no el salto
automtico de lnea. Evitndolo y alterando la posicin actual y el margen,
se consigue la disposicin deseada en columnas.
<br>
Por lo dems, poco cambia; se han aadido dos propiedades (atributos) a la clase
para almacenar el nmero de columna y la posicin donde empiezan las columnas, y
la llamada a MultiCell() incluye un ancho de 6 centmetros.
</body>
</html>