egroupware_official/phpgwapi/inc/fpdf/tutorial/tuto6.htm
2004-06-04 06:17:23 +00:00

69 lines
16 KiB
HTML
Executable File

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Links and flowing text</TITLE>
<LINK TYPE="text/css" REL="stylesheet" HREF="../fpdf.css">
</HEAD>
<BODY>
<H2>Links and flowing text</H2>
This tutorial explains how to insert links (internal and external) and shows a new text writing
mode. It also contains a rudimentary HTML parser.
<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>define<font class="kw">(</font><font class="str">'FPDF_FONTPATH'</font><font class="kw">,</font><font class="str">'font/'</font><font class="kw">);<br>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>var </font>$B<font class="kw">;<br>var </font>$I<font class="kw">;<br>var </font>$U<font class="kw">;<br>var </font>$HREF<font class="kw">;<br><br>function </font>PDF<font class="kw">(</font>$orientation<font class="kw">=</font><font class="str">'P'</font><font class="kw">,</font>$unit<font class="kw">=</font><font class="str">'mm'</font><font class="kw">,</font>$format<font class="kw">=</font><font class="str">'A4'</font><font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Call parent constructor<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>FPDF<font class="kw">(</font>$orientation<font class="kw">,</font>$unit<font class="kw">,</font>$format<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Initialization<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>B<font class="kw">=</font>0<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>I<font class="kw">=</font>0<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>U<font class="kw">=</font>0<font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>HREF<font class="kw">=</font><font class="str">''</font><font class="kw">;<br>}<br><br>function </font>WriteHTML<font class="kw">(</font>$html<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//HTML parser<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$html<font class="kw">=</font>str_replace<font class="kw">(</font><font class="str">"\n"</font><font class="kw">,</font><font class="str">' '</font><font class="kw">,</font>$html<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$a<font class="kw">=</font>preg_split<font class="kw">(</font><font class="str">'/&lt;(.*)&gt;/U'</font><font class="kw">,</font>$html<font class="kw">,-</font>1<font class="kw">,</font>PREG_SPLIT_DELIM_CAPTURE<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;foreach(</font>$a <font class="kw">as </font>$i<font class="kw">=&gt;</font>$e<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</font>$i<font class="kw">%</font>2<font class="kw">==</font>0<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Text<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">if(</font>$<font class="kw">this-&gt;</font>HREF<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>PutLink<font class="kw">(</font>$<font class="kw">this-&gt;</font>HREF<font class="kw">,</font>$e<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Write<font class="kw">(</font>5<font class="kw">,</font>$e<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Tag<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">if(</font>$e<font class="kw">{</font>0<font class="kw">}==</font><font class="str">'/'</font><font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>CloseTag<font class="kw">(</font>strtoupper<font class="kw">(</font>substr<font class="kw">(</font>$e<font class="kw">,</font>1<font class="kw">)));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Extract attributes<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$a2<font class="kw">=</font>explode<font class="kw">(</font><font class="str">' '</font><font class="kw">,</font>$e<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$tag<font class="kw">=</font>strtoupper<font class="kw">(</font>array_shift<font class="kw">(</font>$a2<font class="kw">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$attr<font class="kw">=array();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(</font>$a2 <font class="kw">as </font>$v<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</font>ereg<font class="kw">(</font><font class="str">'^([^=]*)=["\']?([^"\']*)["\']?$'</font><font class="kw">,</font>$v<font class="kw">,</font>$a3<font class="kw">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$attr<font class="kw">[</font>strtoupper<font class="kw">(</font>$a3<font class="kw">[</font>1<font class="kw">])]=</font>$a3<font class="kw">[</font>2<font class="kw">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>OpenTag<font class="kw">(</font>$tag<font class="kw">,</font>$attr<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>function </font>OpenTag<font class="kw">(</font>$tag<font class="kw">,</font>$attr<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Opening tag<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">if(</font>$tag<font class="kw">==</font><font class="str">'B' </font><font class="kw">or </font>$tag<font class="kw">==</font><font class="str">'I' </font><font class="kw">or </font>$tag<font class="kw">==</font><font class="str">'U'</font><font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetStyle<font class="kw">(</font>$tag<font class="kw">,</font>true<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;if(</font>$tag<font class="kw">==</font><font class="str">'A'</font><font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>HREF<font class="kw">=</font>$attr<font class="kw">[</font><font class="str">'HREF'</font><font class="kw">];<br>&nbsp;&nbsp;&nbsp;&nbsp;if(</font>$tag<font class="kw">==</font><font class="str">'BR'</font><font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">(</font>5<font class="kw">);<br>}<br><br>function </font>CloseTag<font class="kw">(</font>$tag<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Closing tag<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">if(</font>$tag<font class="kw">==</font><font class="str">'B' </font><font class="kw">or </font>$tag<font class="kw">==</font><font class="str">'I' </font><font class="kw">or </font>$tag<font class="kw">==</font><font class="str">'U'</font><font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetStyle<font class="kw">(</font>$tag<font class="kw">,</font>false<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;if(</font>$tag<font class="kw">==</font><font class="str">'A'</font><font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>HREF<font class="kw">=</font><font class="str">''</font><font class="kw">;<br>}<br><br>function </font>SetStyle<font class="kw">(</font>$tag<font class="kw">,</font>$enable<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Modify style and select corresponding font<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>$tag<font class="kw">+=(</font>$enable <font class="kw">? </font>1 <font class="kw">: -</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$style<font class="kw">=</font><font class="str">''</font><font class="kw">;<br>&nbsp;&nbsp;&nbsp;&nbsp;foreach(array(</font><font class="str">'B'</font><font class="kw">,</font><font class="str">'I'</font><font class="kw">,</font><font class="str">'U'</font><font class="kw">) as </font>$s<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</font>$<font class="kw">this-&gt;</font>$s<font class="kw">&gt;</font>0<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$style<font class="kw">.=</font>$s<font class="kw">;<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>$style<font class="kw">);<br>}<br><br>function </font>PutLink<font class="kw">(</font>$URL<font class="kw">,</font>$txt<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Put a hyperlink<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetTextColor<font class="kw">(</font>0<font class="kw">,</font>0<font class="kw">,</font>255<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetStyle<font class="kw">(</font><font class="str">'U'</font><font class="kw">,</font>true<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Write<font class="kw">(</font>5<font class="kw">,</font>$txt<font class="kw">,</font>$URL<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetStyle<font class="kw">(</font><font class="str">'U'</font><font class="kw">,</font>false<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>SetTextColor<font class="kw">(</font>0<font class="kw">);<br>}<br>}<br><br></font>$html<font class="kw">=</font><font class="str">'You can now easily print text mixing different<br>styles : &lt;B&gt;bold&lt;/B&gt;, &lt;I&gt;italic&lt;/I&gt;, &lt;U&gt;underlined&lt;/U&gt;, or<br>&lt;B&gt;&lt;I&gt;&lt;U&gt;all at once&lt;/U&gt;&lt;/I&gt;&lt;/B&gt;!&lt;BR&gt;You can also insert links<br>on text, such as &lt;A HREF="http://www.fpdf.org"&gt;www.fpdf.org&lt;/A&gt;,<br>or on an image: click on the logo.'</font><font class="kw">;<br><br></font>$pdf<font class="kw">=new </font>PDF<font class="kw">();<br></font><font class="cmt">//First page<br></font>$pdf<font class="kw">-&gt;</font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-&gt;</font>SetFont<font class="kw">(</font><font class="str">'Arial'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font>20<font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>Write<font class="kw">(</font>5<font class="kw">,</font><font class="str">'To find out what\'s new in this tutorial, click '</font><font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>SetFont<font class="kw">(</font><font class="str">''</font><font class="kw">,</font><font class="str">'U'</font><font class="kw">);<br></font>$link<font class="kw">=</font>$pdf<font class="kw">-&gt;</font>AddLink<font class="kw">();<br></font>$pdf<font class="kw">-&gt;</font>Write<font class="kw">(</font>5<font class="kw">,</font><font class="str">'here'</font><font class="kw">,</font>$link<font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>SetFont<font class="kw">(</font><font class="str">''</font><font class="kw">);<br></font><font class="cmt">//Second page<br></font>$pdf<font class="kw">-&gt;</font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-&gt;</font>SetLink<font class="kw">(</font>$link<font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>Image<font class="kw">(</font><font class="str">'logo.png'</font><font class="kw">,</font>10<font class="kw">,</font>10<font class="kw">,</font>30<font class="kw">,</font>0<font class="kw">,</font><font class="str">''</font><font class="kw">,</font><font class="str">'http://www.fpdf.org'</font><font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>SetLeftMargin<font class="kw">(</font>45<font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>SetFontSize<font class="kw">(</font>14<font class="kw">);<br></font>$pdf<font class="kw">-&gt;</font>WriteHTML<font class="kw">(</font>$html<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='tuto6.php' TARGET='_blank' CLASS='demo'>[Demo]</A></P>");
}
//-->
</SCRIPT>
The new method to print text is <A HREF='../doc/write.htm'>Write()</A>. It is very close to <A HREF='../doc/multicell.htm'>MultiCell()</A>; the differences
are:
<UL>
<LI>The end of line is at the right margin and the next line begins at the left one
<LI>The current position moves at the end of the text
</UL>
So it allows to write a chunk of text, alter the font style, then continue from the exact
place we left it. On the other hand, you cannot full justify it.
<BR>
<BR>
The method is used on the first page to put a link pointing to the second one. The beginning of
the sentence is written in regular style, then we switch to underline and finish it. The link
is created with <A HREF='../doc/addlink.htm'>AddLink()</A>, which returns a link identifier. The identifier is
passed as third parameter of Write(). Once the second page is created, we use <A HREF='../doc/setlink.htm'>SetLink()</A> to
make the link point to the beginning of the current page.
<BR>
<BR>
Then we put an image with a link on it. An external link points to an URL (HTTP, mailto...).
The URL is simply passed as last parameter of <A HREF='../doc/image.htm'>Image()</A>.
Note that external links do not work when the PDF is displayed inside Netscape's plug-in.
<BR>
<BR>
Finally, the left margin is moved after the image with <A HREF='../doc/setleftmargin.htm'>SetLeftMargin()</A> and some text in
HTML format is output. An HTML parser is used for this, based on the regular expression splitting
function preg_split() and the option PREG_SPLIT_DELIM_CAPTURE (introduced in PHP 4.0.5) which
allows to fetch the separators as well (in this case the tags). If you use an older version of
PHP, replace the line with this one:
<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">
$a<font class="kw">=</font>preg_split<font class="kw">(</font><font class="str">'/[&lt;&gt;]/'</font><font class="kw">,</font>$html<font class="kw">);</font><br>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
which is less strict but gives the same results with valid HTML.
<BR>
Recognized tags are &lt;B&gt;, &lt;I&gt;, &lt;U&gt;, &lt;A&gt; and &lt;BR&gt;; the others are
ignored. The parser also makes use of the Write() method. An external link is put the same way as
an internal one (third parameter of Write()).
<BR>
Note that <A HREF='../doc/cell.htm'>Cell()</A> also allows to put links.
</BODY>
</HTML>