source: trunk/client/inc/hpdf/_fpdf/tutorial/tuto4.htm@ 194

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

importo il progetto

File size: 9.6 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>Multi-columns</title>
6<link type="text/css" rel="stylesheet" href="../fpdf.css">
7</head>
8<body>
9<h1>Multi-columns</h1>
10This example is a variant of the previous one showing how to lay the text across multiple
11columns.
12<div class="source">
13<pre><code>&lt;?php
14<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
15
16class </span>PDF <span class="kw">extends </span>FPDF
17<span class="kw">{
18</span><span class="cmt">//Current column
19</span><span class="kw">var </span>$col<span class="kw">=</span>0<span class="kw">;
20</span><span class="cmt">//Ordinate of column start
21</span><span class="kw">var </span>$y0<span class="kw">;
22
23function </span>Header<span class="kw">()
24{
25 </span><span class="cmt">//Page header
26 </span><span class="kw">global </span>$title<span class="kw">;
27
28 </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">);
29 </span>$w<span class="kw">=</span>$<span class="kw">this-&gt;</span>GetStringWidth<span class="kw">(</span>$title<span class="kw">)+</span>6<span class="kw">;
30 </span>$<span class="kw">this-&gt;</span>SetX<span class="kw">((</span>210<span class="kw">-</span>$w<span class="kw">)/</span>2<span class="kw">);
31 </span>$<span class="kw">this-&gt;</span>SetDrawColor<span class="kw">(</span>0<span class="kw">,</span>80<span class="kw">,</span>180<span class="kw">);
32 </span>$<span class="kw">this-&gt;</span>SetFillColor<span class="kw">(</span>230<span class="kw">,</span>230<span class="kw">,</span>0<span class="kw">);
33 </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>220<span class="kw">,</span>50<span class="kw">,</span>50<span class="kw">);
34 </span>$<span class="kw">this-&gt;</span>SetLineWidth<span class="kw">(</span>1<span class="kw">);
35 </span>$<span class="kw">this-&gt;</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">);
36 </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>10<span class="kw">);
37 </span><span class="cmt">//Save ordinate
38 </span>$<span class="kw">this-&gt;</span>y0<span class="kw">=</span>$<span class="kw">this-&gt;</span>GetY<span class="kw">();
39}
40
41function </span>Footer<span class="kw">()
42{
43 </span><span class="cmt">//Page footer
44 </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(-</span>15<span class="kw">);
45 </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">);
46 </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>128<span class="kw">);
47 </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>0<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
48}
49
50function </span>SetCol<span class="kw">(</span>$col<span class="kw">)
51{
52 </span><span class="cmt">//Set position at a given column
53 </span>$<span class="kw">this-&gt;</span>col<span class="kw">=</span>$col<span class="kw">;
54 </span>$x<span class="kw">=</span>10<span class="kw">+</span>$col<span class="kw">*</span>65<span class="kw">;
55 </span>$<span class="kw">this-&gt;</span>SetLeftMargin<span class="kw">(</span>$x<span class="kw">);
56 </span>$<span class="kw">this-&gt;</span>SetX<span class="kw">(</span>$x<span class="kw">);
57}
58
59function </span>AcceptPageBreak<span class="kw">()
60{
61 </span><span class="cmt">//Method accepting or not automatic page break
62 </span><span class="kw">if(</span>$<span class="kw">this-&gt;</span>col<span class="kw">&lt;</span>2<span class="kw">)
63 {
64 </span><span class="cmt">//Go to next column
65 </span>$<span class="kw">this-&gt;</span>SetCol<span class="kw">(</span>$<span class="kw">this-&gt;</span>col<span class="kw">+</span>1<span class="kw">);
66 </span><span class="cmt">//Set ordinate to top
67 </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(</span>$<span class="kw">this-&gt;</span>y0<span class="kw">);
68 </span><span class="cmt">//Keep on page
69 </span><span class="kw">return </span>false<span class="kw">;
70 }
71 else
72 {
73 </span><span class="cmt">//Go back to first column
74 </span>$<span class="kw">this-&gt;</span>SetCol<span class="kw">(</span>0<span class="kw">);
75 </span><span class="cmt">//Page break
76 </span><span class="kw">return </span>true<span class="kw">;
77 }
78}
79
80function </span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$label<span class="kw">)
81{
82 </span><span class="cmt">//Title
83 </span>$<span class="kw">this-&gt;</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">);
84 </span>$<span class="kw">this-&gt;</span>SetFillColor<span class="kw">(</span>200<span class="kw">,</span>220<span class="kw">,</span>255<span class="kw">);
85 </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>6<span class="kw">,</span><span class="str">"Chapter $num : $label"</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">);
86 </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>4<span class="kw">);
87 </span><span class="cmt">//Save ordinate
88 </span>$<span class="kw">this-&gt;</span>y0<span class="kw">=</span>$<span class="kw">this-&gt;</span>GetY<span class="kw">();
89}
90
91function </span>ChapterBody<span class="kw">(</span>$file<span class="kw">)
92{
93 </span><span class="cmt">//Read text file
94 </span>$f<span class="kw">=</span>fopen<span class="kw">(</span>$file<span class="kw">,</span><span class="str">'r'</span><span class="kw">);
95 </span>$txt<span class="kw">=</span>fread<span class="kw">(</span>$f<span class="kw">,</span>filesize<span class="kw">(</span>$file<span class="kw">));
96 </span>fclose<span class="kw">(</span>$f<span class="kw">);
97 </span><span class="cmt">//Font
98 </span>$<span class="kw">this-&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">);
99 </span><span class="cmt">//Output text in a 6 cm width column
100 </span>$<span class="kw">this-&gt;</span>MultiCell<span class="kw">(</span>60<span class="kw">,</span>5<span class="kw">,</span>$txt<span class="kw">);
101 </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
102 </span><span class="cmt">//Mention
103 </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'I'</span><span class="kw">);
104 </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>5<span class="kw">,</span><span class="str">'(end of excerpt)'</span><span class="kw">);
105 </span><span class="cmt">//Go back to first column
106 </span>$<span class="kw">this-&gt;</span>SetCol<span class="kw">(</span>0<span class="kw">);
107}
108
109function </span>PrintChapter<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">,</span>$file<span class="kw">)
110{
111 </span><span class="cmt">//Add chapter
112 </span>$<span class="kw">this-&gt;</span>AddPage<span class="kw">();
113 </span>$<span class="kw">this-&gt;</span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">);
114 </span>$<span class="kw">this-&gt;</span>ChapterBody<span class="kw">(</span>$file<span class="kw">);
115}
116}
117
118</span>$pdf<span class="kw">=new </span>PDF<span class="kw">();
119</span>$title<span class="kw">=</span><span class="str">'20000 Leagues Under the Seas'</span><span class="kw">;
120</span>$pdf<span class="kw">-&gt;</span>SetTitle<span class="kw">(</span>$title<span class="kw">);
121</span>$pdf<span class="kw">-&gt;</span>SetAuthor<span class="kw">(</span><span class="str">'Jules Verne'</span><span class="kw">);
122</span>$pdf<span class="kw">-&gt;</span>PrintChapter<span class="kw">(</span>1<span class="kw">,</span><span class="str">'A RUNAWAY REEF'</span><span class="kw">,</span><span class="str">'20k_c1.txt'</span><span class="kw">);
123</span>$pdf<span class="kw">-&gt;</span>PrintChapter<span class="kw">(</span>2<span class="kw">,</span><span class="str">'THE PROS AND CONS'</span><span class="kw">,</span><span class="str">'20k_c2.txt'</span><span class="kw">);
124</span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
125</span>?&gt;</code></pre>
126</div>
127<p class='demo'><a href='tuto4.php' target='_blank' class='demo'>[Demo]</a></p>
128The key method used is <a href='../doc/acceptpagebreak.htm'>AcceptPageBreak()</a>. It allows to accept or not an automatic page
129break. By refusing it and altering the margin and current position, the desired column layout
130is achieved.
131<br>
132For the rest, not many changes; two properties have been added to the class to save the current
133column number and the position where columns begin, and the MultiCell() call specifies a
1346 centimeter width.
135</body>
136</html>
Note: See TracBrowser for help on using the repository browser.