Location: PHPKode > scripts > PHP DataGrid > modules/fpdf/tutorial/tuto4.htm
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Multi-columns</TITLE>
<LINK TYPE="text/css" REL="stylesheet" HREF="../fpdf.css">
</HEAD>
<BODY>
<H2>Multi-columns</H2>
This example is a variant of the previous one showing how to lay the text across multiple
columns.
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
&lt;?php<br><font class="kw">require(</font><font class="str">'fpdf.php'</font><font class="kw">);<br><br>class </font>PDF <font class="kw">extends </font>FPDF<br><font class="kw">{<br></font><font class="cmt">//Current column<br></font><font class="kw">var </font>$col<font class="kw">=</font>0<font class="kw">;<br></font><font class="cmt">//Ordinate of column start<br></font><font class="kw">var </font>$y0<font class="kw">;<br><br>function </font>Header<font class="kw">()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Page header<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">global </font>$title<font class="kw">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFont<font class="kw">(</font><font class="str">'Arial'</font><font class="kw">,</font><font class="str">'B'</font><font class="kw">,</font>15<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$w<font class="kw">=</font>$<font class="kw">this-&gt;</font>GetStringWidth<font class="kw">(</font>$title<font class="kw">)+</font>6<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetX<font class="kw">((</font>210<font class="kw">-</font>$w<font class="kw">)/</font>2<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetDrawColor<font class="kw">(</font>0<font class="kw">,</font>80<font class="kw">,</font>180<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFillColor<font class="kw">(</font>230<font class="kw">,</font>230<font class="kw">,</font>0<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetTextColor<font class="kw">(</font>220<font class="kw">,</font>50<font class="kw">,</font>50<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetLineWidth<font class="kw">(</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>$w<font class="kw">,</font>9<font class="kw">,</font>$title<font class="kw">,</font>1<font class="kw">,</font>1<font class="kw">,</font><font class="str">'C'</font><font class="kw">,</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">(</font>10<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Save ordinate<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>y0<font class="kw">=</font>$<font class="kw">this-&gt;</font>GetY<font class="kw">();<br>}<br><br>function </font>Footer<font class="kw">()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Page footer<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetY<font class="kw">(-</font>15<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFont<font class="kw">(</font><font class="str">'Arial'</font><font class="kw">,</font><font class="str">'I'</font><font class="kw">,</font>8<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetTextColor<font class="kw">(</font>128<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>0<font class="kw">,</font>10<font class="kw">,</font><font class="str">'Page '</font><font class="kw">.</font>$<font class="kw">this-&gt;</font>PageNo<font class="kw">(),</font>0<font class="kw">,</font>0<font class="kw">,</font><font class="str">'C'</font><font class="kw">);<br>}<br><br>function </font>SetCol<font class="kw">(</font>$col<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Set position at a given column<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>col<font class="kw">=</font>$col<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$x<font class="kw">=</font>10<font class="kw">+</font>$col<font class="kw">*</font>65<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetLeftMargin<font class="kw">(</font>$x<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetX<font class="kw">(</font>$x<font class="kw">);<br>}<br><br>function </font>AcceptPageBreak<font class="kw">()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Method accepting or not automatic page break<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">if(</font>$<font class="kw">this-&gt;</font>col<font class="kw">&lt;</font>2<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Go to next column<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetCol<font class="kw">(</font>$<font class="kw">this-&gt;</font>col<font class="kw">+</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Set ordinate to top<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetY<font class="kw">(</font>$<font class="kw">this-&gt;</font>y0<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Keep on page<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">return </font>false<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Go back to first column<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetCol<font class="kw">(</font>0<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Page break<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">return </font>true<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>function </font>ChapterTitle<font class="kw">(</font>$num<font class="kw">,</font>$label<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Title<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFont<font class="kw">(</font><font class="str">'Arial'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font>12<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFillColor<font class="kw">(</font>200<font class="kw">,</font>220<font class="kw">,</font>255<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>0<font class="kw">,</font>6<font class="kw">,</font><font class="str">"Chapter&nbsp;&nbsp;$num : $label"</font><font class="kw">,</font>0<font class="kw">,</font>1<font class="kw">,</font><font class="str">'L'</font><font class="kw">,</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">(</font>4<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Save ordinate<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>y0<font class="kw">=</font>$<font class="kw">this-&gt;</font>GetY<font class="kw">();<br>}<br><br>function </font>ChapterBody<font class="kw">(</font>$fichier<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Read text file<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$f<font class="kw">=</font>fopen<font class="kw">(</font>$fichier<font class="kw">,</font><font class="str">'r'</font><font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$txt<font class="kw">=</font>fread<font class="kw">(</font>$f<font class="kw">,</font>filesize<font class="kw">(</font>$fichier<font class="kw">));<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>fclose<font class="kw">(</font>$f<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Font<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFont<font class="kw">(</font><font class="str">'Times'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font>12<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Output text in a 6 cm width column<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>MultiCell<font class="kw">(</font>60<font class="kw">,</font>5<font class="kw">,</font>$txt<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">();<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Mention<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetFont<font class="kw">(</font><font class="str">''</font><font class="kw">,</font><font class="str">'I'</font><font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>0<font class="kw">,</font>5<font class="kw">,</font><font class="str">'(end of excerpt)'</font><font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Go back to first column<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetCol<font class="kw">(</font>0<font class="kw">);<br>}<br><br>function </font>PrintChapter<font class="kw">(</font>$num<font class="kw">,</font>$title<font class="kw">,</font>$file<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Add chapter<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>AddPage<font class="kw">();<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>ChapterTitle<font class="kw">(</font>$num<font class="kw">,</font>$title<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>ChapterBody<font class="kw">(</font>$file<font class="kw">);<br>}<br>}<br><br></font>$pdf<font class="kw">=new </font>PDF<font class="kw">();<br></font>$title<font class="kw">=</font><font class="str">'20000 Leagues Under the Seas'</font><font class="kw">;<br></font>$pdf<font class="kw">-&gt;</font>SetTitle<font class="kw">(</font>$title<font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>SetAuthor<font class="kw">(</font><font class="str">'Jules Verne'</font><font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>PrintChapter<font class="kw">(</font>1<font class="kw">,</font><font class="str">'A RUNAWAY REEF'</font><font class="kw">,</font><font class="str">'20k_c1.txt'</font><font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>PrintChapter<font class="kw">(</font>2<font class="kw">,</font><font class="str">'THE PROS AND CONS'</font><font class="kw">,</font><font class="str">'20k_c2.txt'</font><font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>Output<font class="kw">();<br></font>?&gt;
</font>
</code></NOBR></TD></TR></TABLE><P></P>
<SCRIPT>
<!--
if(document.location.href.indexOf('http:')==0)
{
document.write("<P CLASS='demo'><A HREF='tuto4.php' TARGET='_blank' CLASS='demo'>[Demo]</A></P>");
}
//-->
</SCRIPT>
The key method used is <A HREF='../doc/acceptpagebreak.htm'>AcceptPageBreak()</A>. It allows to accept or not an automatic page
break. By refusing it and altering the margin and current position, the desired column layout
is achieved.
<BR>
For the rest, not much change; two properties have been added to the class to save the current
column number and the position where columns begin, and the MultiCell() call specifies a
6 centimeter width.
</BODY>
</HTML>
Return current item: PHP DataGrid