mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-11-27 18:33:39 +01:00
69 lines
16 KiB
HTML
Executable File
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">
|
|
<?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> </font><font class="cmt">//Call parent constructor<br> </font>$<font class="kw">this-></font>FPDF<font class="kw">(</font>$orientation<font class="kw">,</font>$unit<font class="kw">,</font>$format<font class="kw">);<br> </font><font class="cmt">//Initialization<br> </font>$<font class="kw">this-></font>B<font class="kw">=</font>0<font class="kw">;<br> </font>$<font class="kw">this-></font>I<font class="kw">=</font>0<font class="kw">;<br> </font>$<font class="kw">this-></font>U<font class="kw">=</font>0<font class="kw">;<br> </font>$<font class="kw">this-></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> </font><font class="cmt">//HTML parser<br> </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> </font>$a<font class="kw">=</font>preg_split<font class="kw">(</font><font class="str">'/<(.*)>/U'</font><font class="kw">,</font>$html<font class="kw">,-</font>1<font class="kw">,</font>PREG_SPLIT_DELIM_CAPTURE<font class="kw">);<br> foreach(</font>$a <font class="kw">as </font>$i<font class="kw">=></font>$e<font class="kw">)<br> {<br> if(</font>$i<font class="kw">%</font>2<font class="kw">==</font>0<font class="kw">)<br> {<br> </font><font class="cmt">//Text<br> </font><font class="kw">if(</font>$<font class="kw">this-></font>HREF<font class="kw">)<br> </font>$<font class="kw">this-></font>PutLink<font class="kw">(</font>$<font class="kw">this-></font>HREF<font class="kw">,</font>$e<font class="kw">);<br> else<br> </font>$<font class="kw">this-></font>Write<font class="kw">(</font>5<font class="kw">,</font>$e<font class="kw">);<br> }<br> else<br> {<br> </font><font class="cmt">//Tag<br> </font><font class="kw">if(</font>$e<font class="kw">{</font>0<font class="kw">}==</font><font class="str">'/'</font><font class="kw">)<br> </font>$<font class="kw">this-></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> else<br> {<br> </font><font class="cmt">//Extract attributes<br> </font>$a2<font class="kw">=</font>explode<font class="kw">(</font><font class="str">' '</font><font class="kw">,</font>$e<font class="kw">);<br> </font>$tag<font class="kw">=</font>strtoupper<font class="kw">(</font>array_shift<font class="kw">(</font>$a2<font class="kw">));<br> </font>$attr<font class="kw">=array();<br> foreach(</font>$a2 <font class="kw">as </font>$v<font class="kw">)<br> 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> </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> </font>$<font class="kw">this-></font>OpenTag<font class="kw">(</font>$tag<font class="kw">,</font>$attr<font class="kw">);<br> }<br> }<br> }<br>}<br><br>function </font>OpenTag<font class="kw">(</font>$tag<font class="kw">,</font>$attr<font class="kw">)<br>{<br> </font><font class="cmt">//Opening tag<br> </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> </font>$<font class="kw">this-></font>SetStyle<font class="kw">(</font>$tag<font class="kw">,</font>true<font class="kw">);<br> if(</font>$tag<font class="kw">==</font><font class="str">'A'</font><font class="kw">)<br> </font>$<font class="kw">this-></font>HREF<font class="kw">=</font>$attr<font class="kw">[</font><font class="str">'HREF'</font><font class="kw">];<br> if(</font>$tag<font class="kw">==</font><font class="str">'BR'</font><font class="kw">)<br> </font>$<font class="kw">this-></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> </font><font class="cmt">//Closing tag<br> </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> </font>$<font class="kw">this-></font>SetStyle<font class="kw">(</font>$tag<font class="kw">,</font>false<font class="kw">);<br> if(</font>$tag<font class="kw">==</font><font class="str">'A'</font><font class="kw">)<br> </font>$<font class="kw">this-></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> </font><font class="cmt">//Modify style and select corresponding font<br> </font>$<font class="kw">this-></font>$tag<font class="kw">+=(</font>$enable <font class="kw">? </font>1 <font class="kw">: -</font>1<font class="kw">);<br> </font>$style<font class="kw">=</font><font class="str">''</font><font class="kw">;<br> 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> if(</font>$<font class="kw">this-></font>$s<font class="kw">></font>0<font class="kw">)<br> </font>$style<font class="kw">.=</font>$s<font class="kw">;<br> </font>$<font class="kw">this-></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> </font><font class="cmt">//Put a hyperlink<br> </font>$<font class="kw">this-></font>SetTextColor<font class="kw">(</font>0<font class="kw">,</font>0<font class="kw">,</font>255<font class="kw">);<br> </font>$<font class="kw">this-></font>SetStyle<font class="kw">(</font><font class="str">'U'</font><font class="kw">,</font>true<font class="kw">);<br> </font>$<font class="kw">this-></font>Write<font class="kw">(</font>5<font class="kw">,</font>$txt<font class="kw">,</font>$URL<font class="kw">);<br> </font>$<font class="kw">this-></font>SetStyle<font class="kw">(</font><font class="str">'U'</font><font class="kw">,</font>false<font class="kw">);<br> </font>$<font class="kw">this-></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 : <B>bold</B>, <I>italic</I>, <U>underlined</U>, or<br><B><I><U>all at once</U></I></B>!<BR>You can also insert links<br>on text, such as <A HREF="http://www.fpdf.org">www.fpdf.org</A>,<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">-></font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-></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">-></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">-></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">-></font>AddLink<font class="kw">();<br></font>$pdf<font class="kw">-></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">-></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">-></font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-></font>SetLink<font class="kw">(</font>$link<font class="kw">);<br></font>$pdf<font class="kw">-></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">-></font>SetLeftMargin<font class="kw">(</font>45<font class="kw">);<br></font>$pdf<font class="kw">-></font>SetFontSize<font class="kw">(</font>14<font class="kw">);<br></font>$pdf<font class="kw">-></font>WriteHTML<font class="kw">(</font>$html<font class="kw">);<br></font>$pdf<font class="kw">-></font>Output<font class="kw">();<br></font>?>
|
|
</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">'/[<>]/'</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 <B>, <I>, <U>, <A> and <BR>; 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>
|