| 1 |
<?php |
| 2 |
//HTML2PDF by Cl�ment Lavoillotte |
| 3 |
//ac.lavoillotte@noos.fr |
| 4 |
//webmaster@streetpc.tk |
| 5 |
//http://www.streetpc.tk |
| 6 |
|
| 7 |
require(WPPM_ABSPATH.'asset/lib/fpdf/fpdf.php'); |
| 8 |
|
| 9 |
//function hex2dec |
| 10 |
//returns an associative array (keys: R,G,B) from |
| 11 |
//a hex html code (e.g. #3FE5AA) |
| 12 |
function hex2dec($couleur = "#000000"){ |
| 13 |
$R = substr($couleur, 1, 2); |
| 14 |
$rouge = hexdec($R); |
| 15 |
$V = substr($couleur, 3, 2); |
| 16 |
$vert = hexdec($V); |
| 17 |
$B = substr($couleur, 5, 2); |
| 18 |
$bleu = hexdec($B); |
| 19 |
$tbl_couleur = array(); |
| 20 |
$tbl_couleur['R']=$rouge; |
| 21 |
$tbl_couleur['V']=$vert; |
| 22 |
$tbl_couleur['B']=$bleu; |
| 23 |
return $tbl_couleur; |
| 24 |
} |
| 25 |
|
| 26 |
//conversion pixel -> millimeter at 72 dpi |
| 27 |
function px2mm($px){ |
| 28 |
return $px*25.4/72; |
| 29 |
} |
| 30 |
|
| 31 |
function txtentities($html){ |
| 32 |
$trans = get_html_translation_table(HTML_ENTITIES); |
| 33 |
$trans = array_flip($trans); |
| 34 |
return strtr($html, $trans); |
| 35 |
} |
| 36 |
//////////////////////////////////// |
| 37 |
|
| 38 |
class PDF_HTML extends FPDF |
| 39 |
{ |
| 40 |
//variables of html parser |
| 41 |
protected $B; |
| 42 |
protected $I; |
| 43 |
protected $U; |
| 44 |
protected $HREF; |
| 45 |
protected $fontlist; |
| 46 |
protected $issetfont; |
| 47 |
protected $issetcolor; |
| 48 |
|
| 49 |
function __construct($orientation='P', $unit='mm', $size='A4') |
| 50 |
{ |
| 51 |
//Call parent constructor |
| 52 |
parent::__construct($orientation,$unit,$size); |
| 53 |
//Initialization |
| 54 |
$this->B=0; |
| 55 |
$this->I=0; |
| 56 |
$this->U=0; |
| 57 |
$this->HREF=''; |
| 58 |
$this->fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol'); |
| 59 |
$this->issetfont=false; |
| 60 |
$this->issetcolor=false; |
| 61 |
} |
| 62 |
|
| 63 |
function WriteHTML($html) |
| 64 |
{ |
| 65 |
//HTML parser |
| 66 |
$html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote>"); //supprime tous les tags sauf ceux reconnus |
| 67 |
$html=str_replace("\n",' ',$html); //remplace retour � la ligne par un espace |
| 68 |
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //�clate la cha�ne avec les balises |
| 69 |
foreach($a as $i=>$e) |
| 70 |
{ |
| 71 |
if($i%2==0) |
| 72 |
{ |
| 73 |
//Text |
| 74 |
if($this->HREF) |
| 75 |
$this->PutLink($this->HREF,$e); |
| 76 |
else |
| 77 |
$this->Write(5,txtentities($e)); |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
//Tag |
| 82 |
if($e[0]=='/') |
| 83 |
$this->CloseTag(strtoupper(substr($e,1))); |
| 84 |
else |
| 85 |
{ |
| 86 |
//Extract attributes |
| 87 |
$a2=explode(' ',$e); |
| 88 |
$tag=strtoupper(array_shift($a2)); |
| 89 |
$attr=array(); |
| 90 |
foreach($a2 as $v) |
| 91 |
{ |
| 92 |
if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3)) |
| 93 |
$attr[strtoupper($a3[1])]=$a3[2]; |
| 94 |
} |
| 95 |
$this->OpenTag($tag,$attr); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
function OpenTag($tag, $attr) |
| 102 |
{ |
| 103 |
//Opening tag |
| 104 |
switch($tag){ |
| 105 |
case 'STRONG': |
| 106 |
$this->SetStyle('B',true); |
| 107 |
break; |
| 108 |
case 'EM': |
| 109 |
$this->SetStyle('I',true); |
| 110 |
break; |
| 111 |
case 'B': |
| 112 |
case 'I': |
| 113 |
case 'U': |
| 114 |
$this->SetStyle($tag,true); |
| 115 |
break; |
| 116 |
case 'A': |
| 117 |
$this->HREF=$attr['HREF']; |
| 118 |
break; |
| 119 |
case 'IMG': |
| 120 |
if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) { |
| 121 |
if(!isset($attr['WIDTH'])) |
| 122 |
$attr['WIDTH'] = 0; |
| 123 |
if(!isset($attr['HEIGHT'])) |
| 124 |
$attr['HEIGHT'] = 0; |
| 125 |
$this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT'])); |
| 126 |
} |
| 127 |
break; |
| 128 |
case 'TR': |
| 129 |
case 'BLOCKQUOTE': |
| 130 |
case 'BR': |
| 131 |
$this->Ln(5); |
| 132 |
break; |
| 133 |
case 'P': |
| 134 |
$this->Ln(10); |
| 135 |
break; |
| 136 |
case 'FONT': |
| 137 |
if (isset($attr['COLOR']) && $attr['COLOR']!='') { |
| 138 |
$coul=hex2dec($attr['COLOR']); |
| 139 |
$this->SetTextColor($coul['R'],$coul['V'],$coul['B']); |
| 140 |
$this->issetcolor=true; |
| 141 |
} |
| 142 |
if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) { |
| 143 |
$this->SetFont(strtolower($attr['FACE'])); |
| 144 |
$this->issetfont=true; |
| 145 |
} |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
function CloseTag($tag) |
| 151 |
{ |
| 152 |
//Closing tag |
| 153 |
if($tag=='STRONG') |
| 154 |
$tag='B'; |
| 155 |
if($tag=='EM') |
| 156 |
$tag='I'; |
| 157 |
if($tag=='B' || $tag=='I' || $tag=='U') |
| 158 |
$this->SetStyle($tag,false); |
| 159 |
if($tag=='A') |
| 160 |
$this->HREF=''; |
| 161 |
if($tag=='FONT'){ |
| 162 |
if ($this->issetcolor==true) { |
| 163 |
$this->SetTextColor(0); |
| 164 |
} |
| 165 |
if ($this->issetfont) { |
| 166 |
$this->SetFont('arial'); |
| 167 |
$this->issetfont=false; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
function SetStyle($tag, $enable) |
| 173 |
{ |
| 174 |
//Modify style and select corresponding font |
| 175 |
$this->$tag+=($enable ? 1 : -1); |
| 176 |
$style=''; |
| 177 |
foreach(array('B','I','U') as $s) |
| 178 |
{ |
| 179 |
if($this->$s>0) |
| 180 |
$style.=$s; |
| 181 |
} |
| 182 |
$this->SetFont('',$style); |
| 183 |
} |
| 184 |
|
| 185 |
function PutLink($URL, $txt) |
| 186 |
{ |
| 187 |
//Put a hyperlink |
| 188 |
$this->SetTextColor(0,0,255); |
| 189 |
$this->SetStyle('U',true); |
| 190 |
$this->Write(5,$txt,$URL); |
| 191 |
$this->SetStyle('U',false); |
| 192 |
$this->SetTextColor(0); |
| 193 |
} |
| 194 |
|
| 195 |
}//end of class |
| 196 |
?> |
| 197 |
|