tuto4.htm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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>
  10. This example is a variant of the previous one showing how to lay the text across multiple
  11. columns.
  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. class </span>PDF <span class="kw">extends </span>FPDF
  16. <span class="kw">{
  17. protected </span>$col <span class="kw">= </span>0<span class="kw">; </span><span class="cmt">// Current column
  18. </span><span class="kw">protected </span>$y0<span class="kw">; </span><span class="cmt">// Ordinate of column start
  19. </span><span class="kw">function </span>Header<span class="kw">()
  20. {
  21. </span><span class="cmt">// Page header
  22. </span><span class="kw">global </span>$title<span class="kw">;
  23. </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">);
  24. </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">;
  25. </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">);
  26. </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">);
  27. </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">);
  28. </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">);
  29. </span>$<span class="kw">this-&gt;</span>SetLineWidth<span class="kw">(</span>1<span class="kw">);
  30. </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">);
  31. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>10<span class="kw">);
  32. </span><span class="cmt">// Save ordinate
  33. </span>$<span class="kw">this-&gt;</span>y0 <span class="kw">= </span>$<span class="kw">this-&gt;</span>GetY<span class="kw">();
  34. }
  35. function </span>Footer<span class="kw">()
  36. {
  37. </span><span class="cmt">// Page footer
  38. </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(-</span>15<span class="kw">);
  39. </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">);
  40. </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>128<span class="kw">);
  41. </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">);
  42. }
  43. function </span>SetCol<span class="kw">(</span>$col<span class="kw">)
  44. {
  45. </span><span class="cmt">// Set position at a given column
  46. </span>$<span class="kw">this-&gt;</span>col <span class="kw">= </span>$col<span class="kw">;
  47. </span>$x <span class="kw">= </span>10<span class="kw">+</span>$col<span class="kw">*</span>65<span class="kw">;
  48. </span>$<span class="kw">this-&gt;</span>SetLeftMargin<span class="kw">(</span>$x<span class="kw">);
  49. </span>$<span class="kw">this-&gt;</span>SetX<span class="kw">(</span>$x<span class="kw">);
  50. }
  51. function </span>AcceptPageBreak<span class="kw">()
  52. {
  53. </span><span class="cmt">// Method accepting or not automatic page break
  54. </span><span class="kw">if(</span>$<span class="kw">this-&gt;</span>col<span class="kw">&lt;</span>2<span class="kw">)
  55. {
  56. </span><span class="cmt">// Go to next column
  57. </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">);
  58. </span><span class="cmt">// Set ordinate to top
  59. </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(</span>$<span class="kw">this-&gt;</span>y0<span class="kw">);
  60. </span><span class="cmt">// Keep on page
  61. </span><span class="kw">return </span>false<span class="kw">;
  62. }
  63. else
  64. {
  65. </span><span class="cmt">// Go back to first column
  66. </span>$<span class="kw">this-&gt;</span>SetCol<span class="kw">(</span>0<span class="kw">);
  67. </span><span class="cmt">// Page break
  68. </span><span class="kw">return </span>true<span class="kw">;
  69. }
  70. }
  71. function </span>ChapterTitle<span class="kw">(</span>$num<span class="kw">, </span>$label<span class="kw">)
  72. {
  73. </span><span class="cmt">// Title
  74. </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">);
  75. </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">);
  76. </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 </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">);
  77. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>4<span class="kw">);
  78. </span><span class="cmt">// Save ordinate
  79. </span>$<span class="kw">this-&gt;</span>y0 <span class="kw">= </span>$<span class="kw">this-&gt;</span>GetY<span class="kw">();
  80. }
  81. function </span>ChapterBody<span class="kw">(</span>$file<span class="kw">)
  82. {
  83. </span><span class="cmt">// Read text file
  84. </span>$txt <span class="kw">= </span>file_get_contents<span class="kw">(</span>$file<span class="kw">);
  85. </span><span class="cmt">// Font
  86. </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">);
  87. </span><span class="cmt">// Output text in a 6 cm width column
  88. </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">);
  89. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  90. </span><span class="cmt">// Mention
  91. </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">);
  92. </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">);
  93. </span><span class="cmt">// Go back to first column
  94. </span>$<span class="kw">this-&gt;</span>SetCol<span class="kw">(</span>0<span class="kw">);
  95. }
  96. function </span>PrintChapter<span class="kw">(</span>$num<span class="kw">, </span>$title<span class="kw">, </span>$file<span class="kw">)
  97. {
  98. </span><span class="cmt">// Add chapter
  99. </span>$<span class="kw">this-&gt;</span>AddPage<span class="kw">();
  100. </span>$<span class="kw">this-&gt;</span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">);
  101. </span>$<span class="kw">this-&gt;</span>ChapterBody<span class="kw">(</span>$file<span class="kw">);
  102. }
  103. }
  104. </span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
  105. </span>$title <span class="kw">= </span><span class="str">'20000 Leagues Under the Seas'</span><span class="kw">;
  106. </span>$pdf<span class="kw">-&gt;</span>SetTitle<span class="kw">(</span>$title<span class="kw">);
  107. </span>$pdf<span class="kw">-&gt;</span>SetAuthor<span class="kw">(</span><span class="str">'Jules Verne'</span><span class="kw">);
  108. </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">);
  109. </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">);
  110. </span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
  111. </span>?&gt;</code></pre>
  112. </div>
  113. <p class='demo'><a href='tuto4.php' target='_blank' class='demo'>[Demo]</a></p>
  114. The key method used is <a href='../doc/acceptpagebreak.htm'>AcceptPageBreak()</a>. It allows to accept or not an automatic page
  115. break. By refusing it and altering the margin and current position, the desired column layout
  116. is achieved.
  117. <br>
  118. For the rest, not many changes; two properties have been added to the class to save the current
  119. column number and the position where columns begin, and the MultiCell() call specifies a
  120. 6 centimeter width.
  121. </body>
  122. </html>