Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / fpdf16 / tutorial / tuto6.php @ f59acf11

History | View | Annotate | Download (2.4 KB)

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' || $tag=='I' || $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' || $tag=='I' || $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
        {
86
                if($this->$s>0)
87
                        $style.=$s;
88
        }
89
        $this->SetFont('',$style);
90
}
91

    
92
function PutLink($URL,$txt)
93
{
94
        //Put a hyperlink
95
        $this->SetTextColor(0,0,255);
96
        $this->SetStyle('U',true);
97
        $this->Write(5,$txt,$URL);
98
        $this->SetStyle('U',false);
99
        $this->SetTextColor(0);
100
}
101
}
102

    
103
$html='You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
104
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
105
text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
106

    
107
$pdf=new PDF();
108
//First page
109
$pdf->AddPage();
110
$pdf->SetFont('Arial','',20);
111
$pdf->Write(5,'To find out what\'s new in this tutorial, click ');
112
$pdf->SetFont('','U');
113
$link=$pdf->AddLink();
114
$pdf->Write(5,'here',$link);
115
$pdf->SetFont('');
116
//Second page
117
$pdf->AddPage();
118
$pdf->SetLink($link);
119
$pdf->Image('logo.png',10,12,30,0,'','http://www.fpdf.org');
120
$pdf->SetLeftMargin(45);
121
$pdf->SetFontSize(14);
122
$pdf->WriteHTML($html);
123
$pdf->Output();
124
?>