PageRenderTime 66ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/app/web/libraries/fpdf/html2pdf.php

https://github.com/addien-sarmag/mdis
PHP | 279 lines | 240 code | 21 blank | 18 comment | 47 complexity | 83179aa40675bd859255cd91cf12332b MD5 | raw file
  1. <?php
  2. require_once('fpdf.php');
  3. function hex2dec($couleur = "#000000"){
  4. $R = substr($couleur, 1, 2);
  5. $rouge = hexdec($R);
  6. $V = substr($couleur, 3, 2);
  7. $vert = hexdec($V);
  8. $B = substr($couleur, 5, 2);
  9. $bleu = hexdec($B);
  10. $tbl_couleur = array();
  11. $tbl_couleur['R']=$rouge;
  12. $tbl_couleur['G']=$vert;
  13. $tbl_couleur['B']=$bleu;
  14. return $tbl_couleur;
  15. }
  16. //conversion pixel -> millimeter in 72 dpi
  17. function px2mm($px){
  18. return $px*25.4/72;
  19. }
  20. function txtentities($html){
  21. $trans = get_html_translation_table(HTML_ENTITIES);
  22. $trans = array_flip($trans);
  23. return strtr($html, $trans);
  24. }
  25. ////////////////////////////////////
  26. class html2pdf extends myfpdf
  27. {
  28. //variables of html parser
  29. var $B;
  30. var $I;
  31. var $U;
  32. var $HREF;
  33. var $fontList;
  34. var $issetfont;
  35. var $issetcolor;
  36. function html2pdf($orientation='P', $unit='mm', $format='A4')
  37. {
  38. //Call parent constructor
  39. $this->FPDF($orientation,$unit,$format);
  40. //Initialization
  41. $this->B=0;
  42. $this->I=0;
  43. $this->U=0;
  44. $this->HREF='';
  45. $this->tableborder=0;
  46. $this->tdbegin=false;
  47. $this->tdwidth=0;
  48. $this->tdheight=0;
  49. $this->tdalign="L";
  50. $this->tdbgcolor=false;
  51. $this->oldx=0;
  52. $this->oldy=0;
  53. $this->fontlist=array("arial","times","courier","helvetica","symbol");
  54. $this->issetfont=false;
  55. $this->issetcolor=false;
  56. }
  57. //////////////////////////////////////
  58. //html parser
  59. function WriteHTML($html)
  60. {
  61. $html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote><hr><td><tr><table><sup>"); //remove all unsupported tags
  62. $html=str_replace("\n",'',$html); //replace carriage returns by spaces
  63. $html=str_replace("\t",'',$html); //replace carriage returns by spaces
  64. $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //explodes the string
  65. foreach($a as $i=>$e)
  66. {
  67. if($i%2==0)
  68. {
  69. //Text
  70. if($this->HREF)
  71. $this->PutLink($this->HREF,$e);
  72. elseif($this->tdbegin) {
  73. if(trim($e)!='' && $e!="&nbsp;") {
  74. $this->Cell($this->tdwidth,$this->tdheight,$e,$this->tableborder,'',$this->tdalign,$this->tdbgcolor);
  75. }
  76. elseif($e=="&nbsp;") {
  77. $this->Cell($this->tdwidth,$this->tdheight,'',$this->tableborder,'',$this->tdalign,$this->tdbgcolor);
  78. }
  79. }
  80. else
  81. $this->Write(5,stripslashes(txtentities($e)));
  82. }
  83. else
  84. {
  85. //Tag
  86. if($e[0]=='/')
  87. $this->CloseTag(strtoupper(substr($e,1)));
  88. else
  89. {
  90. //Extract attributes
  91. $a2=explode(' ',$e);
  92. $tag=strtoupper(array_shift($a2));
  93. $attr=array();
  94. foreach($a2 as $v)
  95. {
  96. if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
  97. $attr[strtoupper($a3[1])]=$a3[2];
  98. }
  99. $this->OpenTag($tag,$attr);
  100. }
  101. }
  102. }
  103. }
  104. function OpenTag($tag, $attr)
  105. {
  106. //Opening tag
  107. switch($tag){
  108. case 'SUP':
  109. if( !empty($attr['SUP']) ) {
  110. //Set current font to 6pt
  111. $this->SetFont('','',6);
  112. //Start 125cm plus width of cell to the right of left margin
  113. //Superscript "1"
  114. $this->Cell(2,2,$attr['SUP'],0,0,'L');
  115. }
  116. break;
  117. case 'TABLE': // TABLE-BEGIN
  118. if( !empty($attr['BORDER']) ) $this->tableborder=$attr['BORDER'];
  119. else $this->tableborder=0;
  120. break;
  121. case 'TR': //TR-BEGIN
  122. break;
  123. case 'TD': // TD-BEGIN
  124. if( !empty($attr['WIDTH']) ) $this->tdwidth=($attr['WIDTH']/4);
  125. else $this->tdwidth=40; // Set to your own width if you need bigger fixed cells
  126. if( !empty($attr['HEIGHT']) ) $this->tdheight=($attr['HEIGHT']/6);
  127. else $this->tdheight=6; // Set to your own height if you need bigger fixed cells
  128. if( !empty($attr['ALIGN']) ) {
  129. $align=$attr['ALIGN'];
  130. if($align=='LEFT') $this->tdalign='L';
  131. if($align=='CENTER') $this->tdalign='C';
  132. if($align=='RIGHT') $this->tdalign='R';
  133. }
  134. else $this->tdalign='L'; // Set to your own
  135. if( !empty($attr['BGCOLOR']) ) {
  136. $coul=hex2dec($attr['BGCOLOR']);
  137. $this->SetFillColor($coul['R'],$coul['G'],$coul['B']);
  138. $this->tdbgcolor=true;
  139. }
  140. $this->tdbegin=true;
  141. break;
  142. case 'HR':
  143. if( !empty($attr['WIDTH']) )
  144. $Width = $attr['WIDTH'];
  145. else
  146. $Width = $this->w - $this->lMargin-$this->rMargin;
  147. $x = $this->GetX();
  148. $y = $this->GetY();
  149. $this->SetLineWidth(0.2);
  150. $this->Line($x,$y,$x+$Width,$y);
  151. $this->SetLineWidth(0.2);
  152. $this->Ln(1);
  153. break;
  154. case 'STRONG':
  155. $this->SetStyle('B',true);
  156. break;
  157. case 'EM':
  158. $this->SetStyle('I',true);
  159. break;
  160. case 'B':
  161. case 'I':
  162. case 'U':
  163. $this->SetStyle($tag,true);
  164. break;
  165. case 'A':
  166. $this->HREF=$attr['HREF'];
  167. break;
  168. case 'IMG':
  169. if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) {
  170. if(!isset($attr['WIDTH']))
  171. $attr['WIDTH'] = 0;
  172. if(!isset($attr['HEIGHT']))
  173. $attr['HEIGHT'] = 0;
  174. $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
  175. }
  176. break;
  177. case 'BLOCKQUOTE':
  178. case 'BR':
  179. $this->Ln(5);
  180. break;
  181. case 'P':
  182. $this->Ln(10);
  183. break;
  184. case 'FONT':
  185. if (isset($attr['COLOR']) && $attr['COLOR']!='') {
  186. $coul=hex2dec($attr['COLOR']);
  187. $this->SetTextColor($coul['R'],$coul['G'],$coul['B']);
  188. $this->issetcolor=true;
  189. }
  190. if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) {
  191. $this->SetFont(strtolower($attr['FACE']));
  192. $this->issetfont=true;
  193. }
  194. if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist) && isset($attr['SIZE']) && $attr['SIZE']!='') {
  195. $this->SetFont(strtolower($attr['FACE']),'',$attr['SIZE']);
  196. $this->issetfont=true;
  197. }
  198. break;
  199. }
  200. }
  201. function CloseTag($tag)
  202. {
  203. //Closing tag
  204. if($tag=='SUP') {
  205. }
  206. if($tag=='TD') { // TD-END
  207. $this->tdbegin=false;
  208. $this->tdwidth=0;
  209. $this->tdheight=0;
  210. $this->tdalign="L";
  211. $this->tdbgcolor=false;
  212. }
  213. if($tag=='TR') { // TR-END
  214. $this->Ln();
  215. }
  216. if($tag=='TABLE') { // TABLE-END
  217. //$this->Ln();
  218. $this->tableborder=0;
  219. }
  220. if($tag=='STRONG')
  221. $tag='B';
  222. if($tag=='EM')
  223. $tag='I';
  224. if($tag=='B' || $tag=='I' || $tag=='U')
  225. $this->SetStyle($tag,false);
  226. if($tag=='A')
  227. $this->HREF='';
  228. if($tag=='FONT'){
  229. if ($this->issetcolor==true) {
  230. $this->SetTextColor(0);
  231. }
  232. if ($this->issetfont) {
  233. $this->SetFont('arial');
  234. $this->issetfont=false;
  235. }
  236. }
  237. }
  238. function SetStyle($tag, $enable)
  239. {
  240. //Modify style and select corresponding font
  241. $this->$tag+=($enable ? 1 : -1);
  242. $style='';
  243. foreach(array('B','I','U') as $s) {
  244. if($this->$s>0)
  245. $style.=$s;
  246. }
  247. $this->SetFont('',$style);
  248. }
  249. function PutLink($URL, $txt)
  250. {
  251. //Put a hyperlink
  252. $this->SetTextColor(0,0,255);
  253. $this->SetStyle('U',true);
  254. $this->Write(5,$txt,$URL);
  255. $this->SetStyle('U',false);
  256. $this->SetTextColor(0);
  257. }
  258. }//end of class