tuto6.htm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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>Links and flowing text</title>
  6. <link type="text/css" rel="stylesheet" href="../fpdf.css">
  7. </head>
  8. <body>
  9. <h1>Links and flowing text</h1>
  10. This tutorial explains how to insert links (internal and external) and shows a new text writing
  11. mode. It also contains a basic HTML parser.
  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. var </span>$B<span class="kw">;
  18. var </span>$I<span class="kw">;
  19. var </span>$U<span class="kw">;
  20. var </span>$HREF<span class="kw">;
  21. function </span>PDF<span class="kw">(</span>$orientation<span class="kw">=</span><span class="str">'P'</span><span class="kw">, </span>$unit<span class="kw">=</span><span class="str">'mm'</span><span class="kw">, </span>$size<span class="kw">=</span><span class="str">'A4'</span><span class="kw">)
  22. {
  23. </span><span class="cmt">// Call parent constructor
  24. </span>$<span class="kw">this-&gt;</span>FPDF<span class="kw">(</span>$orientation<span class="kw">,</span>$unit<span class="kw">,</span>$size<span class="kw">);
  25. </span><span class="cmt">// Initialization
  26. </span>$<span class="kw">this-&gt;</span>B <span class="kw">= </span>0<span class="kw">;
  27. </span>$<span class="kw">this-&gt;</span>I <span class="kw">= </span>0<span class="kw">;
  28. </span>$<span class="kw">this-&gt;</span>U <span class="kw">= </span>0<span class="kw">;
  29. </span>$<span class="kw">this-&gt;</span>HREF <span class="kw">= </span><span class="str">''</span><span class="kw">;
  30. }
  31. function </span>WriteHTML<span class="kw">(</span>$html<span class="kw">)
  32. {
  33. </span><span class="cmt">// HTML parser
  34. </span>$html <span class="kw">= </span>str_replace<span class="kw">(</span><span class="str">"\n"</span><span class="kw">,</span><span class="str">' '</span><span class="kw">,</span>$html<span class="kw">);
  35. </span>$a <span class="kw">= </span>preg_split<span class="kw">(</span><span class="str">'/&lt;(.*)&gt;/U'</span><span class="kw">,</span>$html<span class="kw">,-</span>1<span class="kw">,</span>PREG_SPLIT_DELIM_CAPTURE<span class="kw">);
  36. foreach(</span>$a <span class="kw">as </span>$i<span class="kw">=&gt;</span>$e<span class="kw">)
  37. {
  38. if(</span>$i<span class="kw">%</span>2<span class="kw">==</span>0<span class="kw">)
  39. {
  40. </span><span class="cmt">// Text
  41. </span><span class="kw">if(</span>$<span class="kw">this-&gt;</span>HREF<span class="kw">)
  42. </span>$<span class="kw">this-&gt;</span>PutLink<span class="kw">(</span>$<span class="kw">this-&gt;</span>HREF<span class="kw">,</span>$e<span class="kw">);
  43. else
  44. </span>$<span class="kw">this-&gt;</span>Write<span class="kw">(</span>5<span class="kw">,</span>$e<span class="kw">);
  45. }
  46. else
  47. {
  48. </span><span class="cmt">// Tag
  49. </span><span class="kw">if(</span>$e<span class="kw">[</span>0<span class="kw">]==</span><span class="str">'/'</span><span class="kw">)
  50. </span>$<span class="kw">this-&gt;</span>CloseTag<span class="kw">(</span>strtoupper<span class="kw">(</span>substr<span class="kw">(</span>$e<span class="kw">,</span>1<span class="kw">)));
  51. else
  52. {
  53. </span><span class="cmt">// Extract attributes
  54. </span>$a2 <span class="kw">= </span>explode<span class="kw">(</span><span class="str">' '</span><span class="kw">,</span>$e<span class="kw">);
  55. </span>$tag <span class="kw">= </span>strtoupper<span class="kw">(</span>array_shift<span class="kw">(</span>$a2<span class="kw">));
  56. </span>$attr <span class="kw">= array();
  57. foreach(</span>$a2 <span class="kw">as </span>$v<span class="kw">)
  58. {
  59. if(</span>preg_match<span class="kw">(</span><span class="str">'/([^=]*)=["\']?([^"\']*)/'</span><span class="kw">,</span>$v<span class="kw">,</span>$a3<span class="kw">))
  60. </span>$attr<span class="kw">[</span>strtoupper<span class="kw">(</span>$a3<span class="kw">[</span>1<span class="kw">])] = </span>$a3<span class="kw">[</span>2<span class="kw">];
  61. }
  62. </span>$<span class="kw">this-&gt;</span>OpenTag<span class="kw">(</span>$tag<span class="kw">,</span>$attr<span class="kw">);
  63. }
  64. }
  65. }
  66. }
  67. function </span>OpenTag<span class="kw">(</span>$tag<span class="kw">, </span>$attr<span class="kw">)
  68. {
  69. </span><span class="cmt">// Opening tag
  70. </span><span class="kw">if(</span>$tag<span class="kw">==</span><span class="str">'B' </span><span class="kw">|| </span>$tag<span class="kw">==</span><span class="str">'I' </span><span class="kw">|| </span>$tag<span class="kw">==</span><span class="str">'U'</span><span class="kw">)
  71. </span>$<span class="kw">this-&gt;</span>SetStyle<span class="kw">(</span>$tag<span class="kw">,</span>true<span class="kw">);
  72. if(</span>$tag<span class="kw">==</span><span class="str">'A'</span><span class="kw">)
  73. </span>$<span class="kw">this-&gt;</span>HREF <span class="kw">= </span>$attr<span class="kw">[</span><span class="str">'HREF'</span><span class="kw">];
  74. if(</span>$tag<span class="kw">==</span><span class="str">'BR'</span><span class="kw">)
  75. </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>5<span class="kw">);
  76. }
  77. function </span>CloseTag<span class="kw">(</span>$tag<span class="kw">)
  78. {
  79. </span><span class="cmt">// Closing tag
  80. </span><span class="kw">if(</span>$tag<span class="kw">==</span><span class="str">'B' </span><span class="kw">|| </span>$tag<span class="kw">==</span><span class="str">'I' </span><span class="kw">|| </span>$tag<span class="kw">==</span><span class="str">'U'</span><span class="kw">)
  81. </span>$<span class="kw">this-&gt;</span>SetStyle<span class="kw">(</span>$tag<span class="kw">,</span>false<span class="kw">);
  82. if(</span>$tag<span class="kw">==</span><span class="str">'A'</span><span class="kw">)
  83. </span>$<span class="kw">this-&gt;</span>HREF <span class="kw">= </span><span class="str">''</span><span class="kw">;
  84. }
  85. function </span>SetStyle<span class="kw">(</span>$tag<span class="kw">, </span>$enable<span class="kw">)
  86. {
  87. </span><span class="cmt">// Modify style and select corresponding font
  88. </span>$<span class="kw">this-&gt;</span>$tag <span class="kw">+= (</span>$enable <span class="kw">? </span>1 <span class="kw">: -</span>1<span class="kw">);
  89. </span>$style <span class="kw">= </span><span class="str">''</span><span class="kw">;
  90. foreach(array(</span><span class="str">'B'</span><span class="kw">, </span><span class="str">'I'</span><span class="kw">, </span><span class="str">'U'</span><span class="kw">) as </span>$s<span class="kw">)
  91. {
  92. if(</span>$<span class="kw">this-&gt;</span>$s<span class="kw">&gt;</span>0<span class="kw">)
  93. </span>$style <span class="kw">.= </span>$s<span class="kw">;
  94. }
  95. </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span>$style<span class="kw">);
  96. }
  97. function </span>PutLink<span class="kw">(</span>$URL<span class="kw">, </span>$txt<span class="kw">)
  98. {
  99. </span><span class="cmt">// Put a hyperlink
  100. </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>0<span class="kw">,</span>0<span class="kw">,</span>255<span class="kw">);
  101. </span>$<span class="kw">this-&gt;</span>SetStyle<span class="kw">(</span><span class="str">'U'</span><span class="kw">,</span>true<span class="kw">);
  102. </span>$<span class="kw">this-&gt;</span>Write<span class="kw">(</span>5<span class="kw">,</span>$txt<span class="kw">,</span>$URL<span class="kw">);
  103. </span>$<span class="kw">this-&gt;</span>SetStyle<span class="kw">(</span><span class="str">'U'</span><span class="kw">,</span>false<span class="kw">);
  104. </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>0<span class="kw">);
  105. }
  106. }
  107. </span>$html <span class="kw">= </span><span class="str">'You can now easily print text mixing different styles: &lt;b&gt;bold&lt;/b&gt;, &lt;i&gt;italic&lt;/i&gt;,
  108. &lt;u&gt;underlined&lt;/u&gt;, or &lt;b&gt;&lt;i&gt;&lt;u&gt;all at once&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;!&lt;br&gt;&lt;br&gt;You can also insert links on
  109. text, such as &lt;a href="http://www.fpdf.org"&gt;www.fpdf.org&lt;/a&gt;, or on an image: click on the logo.'</span><span class="kw">;
  110. </span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
  111. </span><span class="cmt">// First page
  112. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  113. </span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>20<span class="kw">);
  114. </span>$pdf<span class="kw">-&gt;</span>Write<span class="kw">(</span>5<span class="kw">,</span><span class="str">"To find out what's new in this tutorial, click "</span><span class="kw">);
  115. </span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'U'</span><span class="kw">);
  116. </span>$link <span class="kw">= </span>$pdf<span class="kw">-&gt;</span>AddLink<span class="kw">();
  117. </span>$pdf<span class="kw">-&gt;</span>Write<span class="kw">(</span>5<span class="kw">,</span><span class="str">'here'</span><span class="kw">,</span>$link<span class="kw">);
  118. </span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">);
  119. </span><span class="cmt">// Second page
  120. </span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
  121. </span>$pdf<span class="kw">-&gt;</span>SetLink<span class="kw">(</span>$link<span class="kw">);
  122. </span>$pdf<span class="kw">-&gt;</span>Image<span class="kw">(</span><span class="str">'logo.png'</span><span class="kw">,</span>10<span class="kw">,</span>12<span class="kw">,</span>30<span class="kw">,</span>0<span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'http://www.fpdf.org'</span><span class="kw">);
  123. </span>$pdf<span class="kw">-&gt;</span>SetLeftMargin<span class="kw">(</span>45<span class="kw">);
  124. </span>$pdf<span class="kw">-&gt;</span>SetFontSize<span class="kw">(</span>14<span class="kw">);
  125. </span>$pdf<span class="kw">-&gt;</span>WriteHTML<span class="kw">(</span>$html<span class="kw">);
  126. </span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
  127. </span>?&gt;</code></pre>
  128. </div>
  129. <p class='demo'><a href='tuto6.php' target='_blank' class='demo'>[Demo]</a></p>
  130. The new method to print text is <a href='../doc/write.htm'>Write()</a>. It's very close to <a href='../doc/multicell.htm'>MultiCell()</a>; the differences are:
  131. <ul>
  132. <li>The end of line is at the right margin and the next line begins at the left one</li>
  133. <li>The current position moves at the end of the text</li>
  134. </ul>
  135. So it allows to write a chunk of text, alter the font style, then continue from the exact
  136. place we left it. On the other hand, you cannot justify it.
  137. <br>
  138. <br>
  139. The method is used on the first page to put a link pointing to the second one. The beginning of
  140. the sentence is written in regular style, then we switch to underline and finish it. The link
  141. is created with <a href='../doc/addlink.htm'>AddLink()</a>, which returns a link identifier. The identifier is
  142. passed as third parameter of Write(). Once the second page is created, we use <a href='../doc/setlink.htm'>SetLink()</a> to
  143. make the link point to the beginning of the current page.
  144. <br>
  145. <br>
  146. Then we put an image with an external link on it. An external link is just a URL. It's passed as
  147. last parameter of <a href='../doc/image.htm'>Image()</a>.
  148. <br>
  149. <br>
  150. Finally, the left margin is moved after the image with <a href='../doc/setleftmargin.htm'>SetLeftMargin()</a> and some text in
  151. HTML format is output. A very simple HTML parser is used for this, based on regular expressions.
  152. Recognized tags are &lt;b&gt;, &lt;i&gt;, &lt;u&gt;, &lt;a&gt; and &lt;br&gt;; the others are
  153. ignored. The parser also makes use of the Write() method. An external link is put the same way as
  154. an internal one (third parameter of Write()). Note that <a href='../doc/cell.htm'>Cell()</a> also allows to put links.
  155. </body>
  156. </html>