pdfHtml.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. //HTML2PDF par Clément Lavoillotte
  3. //ac.lavoillotte@noos.fr
  4. //webmaster@streetpc.tk
  5. //http://www.streetpc.tk
  6. require('fpdf.php');
  7. // fonction hex2dec
  8. // retourne un tableau associatif (clés : R,V,B) à
  9. // partir d'un code html de couleur hexa (ex : #3FE5AA)
  10. function hex2dec($couleur = "#000000"){
  11. $R = substr($couleur, 1, 2);
  12. $rouge = hexdec($R);
  13. $V = substr($couleur, 3, 2);
  14. $vert = hexdec($V);
  15. $B = substr($couleur, 5, 2);
  16. $bleu = hexdec($B);
  17. $tbl_couleur = array();
  18. $tbl_couleur['R']=$rouge;
  19. $tbl_couleur['V']=$vert;
  20. $tbl_couleur['B']=$bleu;
  21. return $tbl_couleur;
  22. }
  23. //conversion pixel -> millimètre en 72 dpi
  24. function px2mm($px){
  25. return $px*25.4/72;
  26. }
  27. function txtentities($html){
  28. $trans = get_html_translation_table(HTML_ENTITIES);
  29. $trans = array_flip($trans);
  30. return strtr($html, $trans);
  31. }
  32. ////////////////////////////////////
  33. class pdfHtml extends FPDF
  34. {
  35. //variables du parseur html
  36. var $B;
  37. var $I;
  38. var $U;
  39. var $HREF;
  40. var $fontList;
  41. var $issetfont;
  42. var $issetcolor;
  43. function PDF_HTML($orientation='P', $unit='mm', $format='A4')
  44. {
  45. //Appel au constructeur parent
  46. $this->FPDF($orientation,$unit,$format);
  47. //Initialisation
  48. $this->B=0;
  49. $this->I=0;
  50. $this->U=0;
  51. $this->HREF='';
  52. $this->fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol');
  53. $this->issetfont=false;
  54. $this->issetcolor=false;
  55. }
  56. function WriteHTML($html)
  57. {
  58. //Parseur HTML
  59. $html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote>"); //supprime tous les tags sauf ceux reconnus
  60. $html=str_replace("\n",' ',$html); //remplace retour à la ligne par un espace
  61. $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //éclate la chaîne avec les balises
  62. foreach($a as $i=>$e)
  63. {
  64. if($i%2==0)
  65. {
  66. //Texte
  67. if($this->HREF)
  68. $this->PutLink($this->HREF,$e);
  69. else
  70. $this->Write(5,stripslashes(txtentities($e)));
  71. }
  72. else
  73. {
  74. //Balise
  75. if($e[0]=='/')
  76. $this->CloseTag(strtoupper(substr($e,1)));
  77. else
  78. {
  79. //Extraction des attributs
  80. $a2=explode(' ',$e);
  81. $tag=strtoupper(array_shift($a2));
  82. $attr=array();
  83. foreach($a2 as $v)
  84. {
  85. if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
  86. $attr[strtoupper($a3[1])]=$a3[2];
  87. }
  88. $this->OpenTag($tag,$attr);
  89. }
  90. }
  91. }
  92. }
  93. function OpenTag($tag, $attr)
  94. {
  95. //Balise ouvrante
  96. switch($tag){
  97. case 'STRONG':
  98. $this->SetStyle('B',true);
  99. break;
  100. case 'EM':
  101. $this->SetStyle('I',true);
  102. break;
  103. case 'B':
  104. case 'I':
  105. case 'U':
  106. $this->SetStyle($tag,true);
  107. break;
  108. case 'A':
  109. $this->HREF=$attr['HREF'];
  110. break;
  111. case 'IMG':
  112. if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) {
  113. if(!isset($attr['WIDTH']))
  114. $attr['WIDTH'] = 0;
  115. if(!isset($attr['HEIGHT']))
  116. $attr['HEIGHT'] = 0;
  117. $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
  118. }
  119. break;
  120. case 'TR':
  121. case 'BLOCKQUOTE':
  122. case 'BR':
  123. $this->Ln(5);
  124. break;
  125. case 'P':
  126. $this->Ln(10);
  127. break;
  128. case 'FONT':
  129. if (isset($attr['COLOR']) && $attr['COLOR']!='') {
  130. $coul=hex2dec($attr['COLOR']);
  131. $this->SetTextColor($coul['R'],$coul['V'],$coul['B']);
  132. $this->issetcolor=true;
  133. }
  134. if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) {
  135. $this->SetFont(strtolower($attr['FACE']));
  136. $this->issetfont=true;
  137. }
  138. break;
  139. }
  140. }
  141. function CloseTag($tag)
  142. {
  143. //Balise fermante
  144. if($tag=='STRONG')
  145. $tag='B';
  146. if($tag=='EM')
  147. $tag='I';
  148. if($tag=='B' || $tag=='I' || $tag=='U')
  149. $this->SetStyle($tag,false);
  150. if($tag=='A')
  151. $this->HREF='';
  152. if($tag=='FONT'){
  153. if ($this->issetcolor==true) {
  154. $this->SetTextColor(0);
  155. }
  156. if ($this->issetfont) {
  157. $this->SetFont('arial');
  158. $this->issetfont=false;
  159. }
  160. }
  161. }
  162. function SetStyle($tag, $enable)
  163. {
  164. //Modifie le style et sélectionne la police correspondante
  165. $this->$tag+=($enable ? 1 : -1);
  166. $style='';
  167. foreach(array('B','I','U') as $s)
  168. {
  169. if($this->$s>0)
  170. $style.=$s;
  171. }
  172. $this->SetFont('',$style);
  173. }
  174. function PutLink($URL, $txt)
  175. {
  176. //Place un hyperlien
  177. $this->SetTextColor(0,0,255);
  178. $this->SetStyle('U',true);
  179. $this->Write(5,$txt,$URL);
  180. $this->SetStyle('U',false);
  181. $this->SetTextColor(0);
  182. }
  183. }//fin classe
  184. ?>