egroupware/phpgwapi/inc/fpdf/tutorial/tuto6.htm

69 lines
16 KiB
HTML
Raw Normal View History

<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>
</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>