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/');
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);
16 $V = substr($couleur, 3, 2);
18 $B = substr($couleur, 5, 2);
20 $tbl_couleur = array();
21 $tbl_couleur['R']=$rouge;
22 $tbl_couleur['G']=$vert;
23 $tbl_couleur['B']=$bleu;
27 //conversion pixel -> millimeter in 72 dpi
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
50 function PDF($orientation='P',$unit='mm',$format='A4')
52 //Call parent constructor
53 $this->TCPDF_Protection($orientation,$unit,$format);
59 $this->fontlist
=array("arial","times","courier","helvetica","symbol");
60 $this->issetfont
=false;
61 $this->issetcolor
=false;
64 //////////////////////////////////////
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
78 $this->PutLink($this->HREF
,$e);
80 $this->Write(5,stripslashes(txtentities($e)));
86 $this->CloseTag(strtoupper(substr($e,1)));
91 $tag=strtoupper(array_shift($a2));
94 if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
95 $attr[strtoupper($a3[1])]=$a3[2];
96 $this->OpenTag($tag,$attr);
102 function OpenTag($tag,$attr)
107 $this->SetStyle('B',true);
110 $this->SetStyle('I',true);
115 $this->SetStyle($tag,true);
118 $this->HREF
=$attr['HREF'];
121 if(isset($attr['SRC']) and (isset($attr['WIDTH']) or isset($attr['HEIGHT']))) {
122 if(!isset($attr['WIDTH']))
124 if(!isset($attr['HEIGHT']))
126 $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
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;
151 function CloseTag($tag)
158 if($tag=='B' or $tag=='I' or $tag=='U')
159 $this->SetStyle($tag,false);
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);
178 foreach(array('B','I','U') as $s)
181 $this->SetFont('',$style);
184 function PutLink($URL,$txt)
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);