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