tuto3.htm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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>Line breaks and colors</title>
  6. <link type="text/css" rel="stylesheet" href="../fpdf.css">
  7. </head>
  8. <body>
  9. <h1>Line breaks and colors</h1>
  10. Let's continue with an example which prints justified paragraphs. It also illustrates the use
  11. of colors.
  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. function </span>Header<span class="kw">()
  18. {
  19. global </span>$title<span class="kw">;
  20. </span><span class="cmt">// Arial bold 15
  21. </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">);
  22. </span><span class="cmt">// Calculate width of title and position
  23. </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">;
  24. </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">);
  25. </span><span class="cmt">// Colors of frame, background and text
  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="cmt">// Thickness of frame (1 mm)
  30. </span>$<span class="kw">this-&gt;</span>SetLineWidth<span class="kw">(</span>1<span class="kw">);
  31. </span><span class="cmt">// Title
  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="cmt">// Line break
  34. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>10<span class="kw">);
  35. }
  36. function </span>Footer<span class="kw">()
  37. {
  38. </span><span class="cmt">// Position at 1.5 cm from bottom
  39. </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(-</span>15<span class="kw">);
  40. </span><span class="cmt">// Arial italic 8
  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="cmt">// Text color in gray
  43. </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>128<span class="kw">);
  44. </span><span class="cmt">// Page number
  45. </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">);
  46. }
  47. function </span>ChapterTitle<span class="kw">(</span>$num<span class="kw">, </span>$label<span class="kw">)
  48. {
  49. </span><span class="cmt">// Arial 12
  50. </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">);
  51. </span><span class="cmt">// Background color
  52. </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">);
  53. </span><span class="cmt">// Title
  54. </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">);
  55. </span><span class="cmt">// Line break
  56. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>4<span class="kw">);
  57. }
  58. function </span>ChapterBody<span class="kw">(</span>$file<span class="kw">)
  59. {
  60. </span><span class="cmt">// Read text file
  61. </span>$txt <span class="kw">= </span>file_get_contents<span class="kw">(</span>$file<span class="kw">);
  62. </span><span class="cmt">// Times 12
  63. </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">);
  64. </span><span class="cmt">// Output justified text
  65. </span>$<span class="kw">this-&gt;</span>MultiCell<span class="kw">(</span>0<span class="kw">,</span>5<span class="kw">,</span>$txt<span class="kw">);
  66. </span><span class="cmt">// Line break
  67. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
  68. </span><span class="cmt">// Mention in italics
  69. </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">);
  70. </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">);
  71. }
  72. function </span>PrintChapter<span class="kw">(</span>$num<span class="kw">, </span>$title<span class="kw">, </span>$file<span class="kw">)
  73. {
  74. </span>$<span class="kw">this-&gt;</span>AddPage<span class="kw">();
  75. </span>$<span class="kw">this-&gt;</span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">);
  76. </span>$<span class="kw">this-&gt;</span>ChapterBody<span class="kw">(</span>$file<span class="kw">);
  77. }
  78. }
  79. </span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
  80. </span>$title <span class="kw">= </span><span class="str">'20000 Leagues Under the Seas'</span><span class="kw">;
  81. </span>$pdf<span class="kw">-&gt;</span>SetTitle<span class="kw">(</span>$title<span class="kw">);
  82. </span>$pdf<span class="kw">-&gt;</span>SetAuthor<span class="kw">(</span><span class="str">'Jules Verne'</span><span class="kw">);
  83. </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">);
  84. </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">);
  85. </span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
  86. </span>?&gt;</code></pre>
  87. </div>
  88. <p class='demo'><a href='tuto3.php' target='_blank' class='demo'>[Demo]</a></p>
  89. The <a href='../doc/getstringwidth.htm'>GetStringWidth()</a> method allows to determine the length of a string in the current font,
  90. which is used here to calculate the position and the width of the frame surrounding the title.
  91. Then colors are set (via <a href='../doc/setdrawcolor.htm'>SetDrawColor()</a>, <a href='../doc/setfillcolor.htm'>SetFillColor()</a> and <a href='../doc/settextcolor.htm'>SetTextColor()</a>) and the
  92. thickness of the line is set to 1 mm (instead of 0.2 by default) with <a href='../doc/setlinewidth.htm'>SetLineWidth()</a>. Finally,
  93. we output the cell (the last parameter <code>true</code> indicates that the background must
  94. be filled).
  95. <br>
  96. <br>
  97. The method used to print the paragraphs is <a href='../doc/multicell.htm'>MultiCell()</a>. Each time a line reaches the
  98. right extremity of the cell or a carriage return character is met, a line break is issued
  99. and a new cell automatically created under the current one. Text is justified by default.
  100. <br>
  101. <br>
  102. Two document properties are defined: the title (<a href='../doc/settitle.htm'>SetTitle()</a>) and the author (<a href='../doc/setauthor.htm'>SetAuthor()</a>).
  103. There are several ways to view them in Adobe Reader. The first one is to open the file directly with
  104. the reader, go to the File menu and choose the Properties option. The second one, also available from
  105. the plug-in, is to right-click and select Document Properties. The third method is to type the Ctrl+D
  106. key combination.
  107. </body>
  108. </html>