tuto4.htm 9.6 KB

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