Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / tcpdf / html2pdf.php
blobacfecb11c4f14ddcb3b9394426cdcce70a681761
1 <?php
2 //HTML2PDF by Clément Lavoillotte
3 //ac.lavoillotte@noos.fr
4 //webmaster@streetpc.tk
5 //http://www.streetpc.tk
7 define('TCPDF_FONTPATH','fonts/');
8 require('tcpdf.php');
10 //function hex2dec
11 //returns an associative array (keys: R,G,B) from
12 //a hex html code (e.g. #3FE5AA)
13 function hex2dec($couleur = "#000000"){
14 $R = substr($couleur, 1, 2);
15 $rouge = hexdec($R);
16 $V = substr($couleur, 3, 2);
17 $vert = hexdec($V);
18 $B = substr($couleur, 5, 2);
19 $bleu = hexdec($B);
20 $tbl_couleur = array();
21 $tbl_couleur['R']=$rouge;
22 $tbl_couleur['G']=$vert;
23 $tbl_couleur['B']=$bleu;
24 return $tbl_couleur;
27 //conversion pixel -> millimeter in 72 dpi
28 function px2mm($px){
29 return $px*25.4/72;
32 function txtentities($html){
33 $trans = get_html_translation_table(HTML_ENTITIES);
34 $trans = array_flip($trans);
35 return strtr($html, $trans);
37 ////////////////////////////////////
39 class PDF extends TCPDF_Protection
41 //variables of html parser
42 var $B;
43 var $I;
44 var $U;
45 var $HREF;
46 var $fontList;
47 var $issetfont;
48 var $issetcolor;
50 function PDF($orientation='P',$unit='mm',$format='A4')
52 //Call parent constructor
53 $this->TCPDF_Protection($orientation,$unit,$format);
54 //Initialization
55 $this->B=0;
56 $this->I=0;
57 $this->U=0;
58 $this->HREF='';
59 $this->fontlist=array("arial","times","courier","helvetica","symbol");
60 $this->issetfont=false;
61 $this->issetcolor=false;
64 //////////////////////////////////////
65 //html parser
67 function WriteHTML($html)
69 $html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote>"); //remove all unsupported tags
70 $html=str_replace("\n",' ',$html); //replace carriage returns by spaces
71 $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //explodes the string
72 foreach($a as $i=>$e)
74 if($i%2==0)
76 //Text
77 if($this->HREF)
78 $this->PutLink($this->HREF,$e);
79 else
80 $this->Write(5,stripslashes(txtentities($e)));
82 else
84 //Tag
85 if($e{0}=='/')
86 $this->CloseTag(strtoupper(substr($e,1)));
87 else
89 //Extract attributes
90 $a2=explode(' ',$e);
91 $tag=strtoupper(array_shift($a2));
92 $attr=array();
93 foreach($a2 as $v)
94 if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
95 $attr[strtoupper($a3[1])]=$a3[2];
96 $this->OpenTag($tag,$attr);
102 function OpenTag($tag,$attr)
104 //Opening tag
105 switch($tag){
106 case 'STRONG':
107 $this->SetStyle('B',true);
108 break;
109 case 'EM':
110 $this->SetStyle('I',true);
111 break;
112 case 'B':
113 case 'I':
114 case 'U':
115 $this->SetStyle($tag,true);
116 break;
117 case 'A':
118 $this->HREF=$attr['HREF'];
119 break;
120 case 'IMG':
121 if(isset($attr['SRC']) and (isset($attr['WIDTH']) or isset($attr['HEIGHT']))) {
122 if(!isset($attr['WIDTH']))
123 $attr['WIDTH'] = 0;
124 if(!isset($attr['HEIGHT']))
125 $attr['HEIGHT'] = 0;
126 $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
128 break;
129 case 'TR':
130 case 'BLOCKQUOTE':
131 case 'BR':
132 $this->Ln(5);
133 break;
134 case 'P':
135 $this->Ln(10);
136 break;
137 case 'FONT':
138 if (isset($attr['COLOR']) and $attr['COLOR']!='') {
139 $coul=hex2dec($attr['COLOR']);
140 $this->SetTextColor($coul['R'],$coul['G'],$coul['B']);
141 $this->issetcolor=true;
143 if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist)) {
144 $this->SetFont(strtolower($attr['FACE']));
145 $this->issetfont=true;
147 break;
151 function CloseTag($tag)
153 //Closing tag
154 if($tag=='STRONG')
155 $tag='B';
156 if($tag=='EM')
157 $tag='I';
158 if($tag=='B' or $tag=='I' or $tag=='U')
159 $this->SetStyle($tag,false);
160 if($tag=='A')
161 $this->HREF='';
162 if($tag=='FONT'){
163 if ($this->issetcolor==true) {
164 $this->SetTextColor(0);
166 if ($this->issetfont) {
167 $this->SetFont('arial');
168 $this->issetfont=false;
173 function SetStyle($tag,$enable)
175 //Modify style and select corresponding font
176 $this->$tag+=($enable ? 1 : -1);
177 $style='';
178 foreach(array('B','I','U') as $s)
179 if($this->$s>0)
180 $style.=$s;
181 $this->SetFont('',$style);
184 function PutLink($URL,$txt)
186 //Put a hyperlink
187 $this->SetTextColor(0,0,255);
188 $this->SetStyle('U',true);
189 $this->Write(5,$txt,$URL);
190 $this->SetStyle('U',false);
191 $this->SetTextColor(0);
194 }//end of class