[2] | 1 | <?php |
---|
| 2 | define('FPDF_FONTPATH','../font/'); |
---|
| 3 | require('../fpdf.php'); |
---|
| 4 | |
---|
| 5 | class PDF extends FPDF |
---|
| 6 | { |
---|
| 7 | var $B; |
---|
| 8 | var $I; |
---|
| 9 | var $U; |
---|
| 10 | var $HREF; |
---|
| 11 | |
---|
| 12 | function PDF($orientation='P',$unit='mm',$format='A4') |
---|
| 13 | { |
---|
| 14 | //Call parent constructor |
---|
| 15 | $this->FPDF($orientation,$unit,$format); |
---|
| 16 | //Initialization |
---|
| 17 | $this->B=0; |
---|
| 18 | $this->I=0; |
---|
| 19 | $this->U=0; |
---|
| 20 | $this->HREF=''; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | function WriteHTML($html) |
---|
| 24 | { |
---|
| 25 | //HTML parser |
---|
| 26 | $html=str_replace("\n",' ',$html); |
---|
| 27 | $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); |
---|
| 28 | foreach($a as $i=>$e) |
---|
| 29 | { |
---|
| 30 | if($i%2==0) |
---|
| 31 | { |
---|
| 32 | //Text |
---|
| 33 | if($this->HREF) |
---|
| 34 | $this->PutLink($this->HREF,$e); |
---|
| 35 | else |
---|
| 36 | $this->Write(5,$e); |
---|
| 37 | } |
---|
| 38 | else |
---|
| 39 | { |
---|
| 40 | //Tag |
---|
| 41 | if($e{0}=='/') |
---|
| 42 | $this->CloseTag(strtoupper(substr($e,1))); |
---|
| 43 | else |
---|
| 44 | { |
---|
| 45 | //Extract attributes |
---|
| 46 | $a2=explode(' ',$e); |
---|
| 47 | $tag=strtoupper(array_shift($a2)); |
---|
| 48 | $attr=array(); |
---|
| 49 | foreach($a2 as $v) |
---|
[5921] | 50 | if(preg_match('/^([^=]*)=["\']?([^"\']*)["\']?$/',$v,$a3)) |
---|
[2] | 51 | $attr[strtoupper($a3[1])]=$a3[2]; |
---|
| 52 | $this->OpenTag($tag,$attr); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | function OpenTag($tag,$attr) |
---|
| 59 | { |
---|
| 60 | //Opening tag |
---|
| 61 | if($tag=='B' or $tag=='I' or $tag=='U') |
---|
| 62 | $this->SetStyle($tag,true); |
---|
| 63 | if($tag=='A') |
---|
| 64 | $this->HREF=$attr['HREF']; |
---|
| 65 | if($tag=='BR') |
---|
| 66 | $this->Ln(5); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function CloseTag($tag) |
---|
| 70 | { |
---|
| 71 | //Closing tag |
---|
| 72 | if($tag=='B' or $tag=='I' or $tag=='U') |
---|
| 73 | $this->SetStyle($tag,false); |
---|
| 74 | if($tag=='A') |
---|
| 75 | $this->HREF=''; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | function SetStyle($tag,$enable) |
---|
| 79 | { |
---|
| 80 | //Modify style and select corresponding font |
---|
| 81 | $this->$tag+=($enable ? 1 : -1); |
---|
| 82 | $style=''; |
---|
| 83 | foreach(array('B','I','U') as $s) |
---|
| 84 | if($this->$s>0) |
---|
| 85 | $style.=$s; |
---|
| 86 | $this->SetFont('',$style); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | function PutLink($URL,$txt) |
---|
| 90 | { |
---|
| 91 | //Put a hyperlink |
---|
| 92 | $this->SetTextColor(0,0,255); |
---|
| 93 | $this->SetStyle('U',true); |
---|
| 94 | $this->Write(5,$txt,$URL); |
---|
| 95 | $this->SetStyle('U',false); |
---|
| 96 | $this->SetTextColor(0); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | $html='You can now easily print text mixing different |
---|
| 101 | styles : <B>bold</B>, <I>italic</I>, <U>underlined</U>, or |
---|
| 102 | <B><I><U>all at once</U></I></B>!<BR>You can also insert links |
---|
| 103 | on text, such as <A HREF="http://www.fpdf.org">www.fpdf.org</A>, |
---|
| 104 | or on an image: click on the logo.'; |
---|
| 105 | |
---|
| 106 | $pdf=new PDF(); |
---|
| 107 | //First page |
---|
| 108 | $pdf->AddPage(); |
---|
| 109 | $pdf->SetFont('Arial','',20); |
---|
| 110 | $pdf->Write(5,'To find out what\'s new in this tutorial, click '); |
---|
| 111 | $pdf->SetFont('','U'); |
---|
| 112 | $link=$pdf->AddLink(); |
---|
| 113 | $pdf->Write(5,'here',$link); |
---|
| 114 | $pdf->SetFont(''); |
---|
| 115 | //Second page |
---|
| 116 | $pdf->AddPage(); |
---|
| 117 | $pdf->SetLink($link); |
---|
| 118 | $pdf->Image('logo.png',10,10,30,0,'','http://www.fpdf.org'); |
---|
| 119 | $pdf->SetLeftMargin(45); |
---|
| 120 | $pdf->SetFontSize(14); |
---|
| 121 | $pdf->WriteHTML($html); |
---|
| 122 | $pdf->Output(); |
---|
| 123 | ?> |
---|