tuto6.php

Go to the documentation of this file.
00001 <?php
00002 require('../fpdf.php');
00003 
00004 class PDF extends FPDF
00005 {
00006 var $B;
00007 var $I;
00008 var $U;
00009 var $HREF;
00010 
00011 function PDF($orientation='P',$unit='mm',$format='A4')
00012 {
00013     //Call parent constructor
00014     $this->FPDF($orientation,$unit,$format);
00015     //Initialization
00016     $this->B=0;
00017     $this->I=0;
00018     $this->U=0;
00019     $this->HREF='';
00020 }
00021 
00022 function WriteHTML($html)
00023 {
00024     //HTML parser
00025     $html=str_replace("\n",' ',$html);
00026     $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
00027     foreach($a as $i=>$e)
00028     {
00029         if($i%2==0)
00030         {
00031             //Text
00032             if($this->HREF)
00033                 $this->PutLink($this->HREF,$e);
00034             else
00035                 $this->Write(5,$e);
00036         }
00037         else
00038         {
00039             //Tag
00040             if($e[0]=='/')
00041                 $this->CloseTag(strtoupper(substr($e,1)));
00042             else
00043             {
00044                 //Extract attributes
00045                 $a2=explode(' ',$e);
00046                 $tag=strtoupper(array_shift($a2));
00047                 $attr=array();
00048                 foreach($a2 as $v)
00049                 {
00050                     if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
00051                         $attr[strtoupper($a3[1])]=$a3[2];
00052                 }
00053                 $this->OpenTag($tag,$attr);
00054             }
00055         }
00056     }
00057 }
00058 
00059 function OpenTag($tag,$attr)
00060 {
00061     //Opening tag
00062     if($tag=='B' || $tag=='I' || $tag=='U')
00063         $this->SetStyle($tag,true);
00064     if($tag=='A')
00065         $this->HREF=$attr['HREF'];
00066     if($tag=='BR')
00067         $this->Ln(5);
00068 }
00069 
00070 function CloseTag($tag)
00071 {
00072     //Closing tag
00073     if($tag=='B' || $tag=='I' || $tag=='U')
00074         $this->SetStyle($tag,false);
00075     if($tag=='A')
00076         $this->HREF='';
00077 }
00078 
00079 function SetStyle($tag,$enable)
00080 {
00081     //Modify style and select corresponding font
00082     $this->$tag+=($enable ? 1 : -1);
00083     $style='';
00084     foreach(array('B','I','U') as $s)
00085     {
00086         if($this->$s>0)
00087             $style.=$s;
00088     }
00089     $this->SetFont('',$style);
00090 }
00091 
00092 function PutLink($URL,$txt)
00093 {
00094     //Put a hyperlink
00095     $this->SetTextColor(0,0,255);
00096     $this->SetStyle('U',true);
00097     $this->Write(5,$txt,$URL);
00098     $this->SetStyle('U',false);
00099     $this->SetTextColor(0);
00100 }
00101 }
00102 
00103 $html='You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
00104 <u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
00105 text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
00106 
00107 $pdf=new PDF();
00108 //First page
00109 $pdf->AddPage();
00110 $pdf->SetFont('Arial','',20);
00111 $pdf->Write(5,'To find out what\'s new in this tutorial, click ');
00112 $pdf->SetFont('','U');
00113 $link=$pdf->AddLink();
00114 $pdf->Write(5,'here',$link);
00115 $pdf->SetFont('');
00116 //Second page
00117 $pdf->AddPage();
00118 $pdf->SetLink($link);
00119 $pdf->Image('logo.png',10,12,30,0,'','http://www.fpdf.org');
00120 $pdf->SetLeftMargin(45);
00121 $pdf->SetFontSize(14);
00122 $pdf->WriteHTML($html);
00123 $pdf->Output();
00124 ?>
 All Data Structures Files Functions Variables Enumerations