PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/modules/pdf/htmltoolkit.php

https://github.com/swat/pragyan
PHP | 253 lines | 216 code | 13 blank | 24 comment | 29 complexity | 71b16636f0e3939eecae655701c00735 MD5 | raw file
  1. <?php
  2. /*
  3. This script is supposed to be used together with the HTML2FPDF.php class
  4. Copyright (C) 2004-2005 Renato Coelho
  5. */
  6. function ConvertColor($color="#000000"){
  7. //returns an associative array (keys: R,G,B) from html code (e.g. #3FE5AA)
  8. //W3C approved color array (disabled)
  9. //static $common_colors = array('black'=>'#000000','silver'=>'#C0C0C0','gray'=>'#808080', 'white'=>'#FFFFFF','maroon'=>'#800000','red'=>'#FF0000','purple'=>'#800080','fuchsia'=>'#FF00FF','green'=>'#008000','lime'=>'#00FF00','olive'=>'#808000','yellow'=>'#FFFF00','navy'=>'#000080', 'blue'=>'#0000FF','teal'=>'#008080','aqua'=>'#00FFFF');
  10. //All color names array
  11. static $common_colors = array('antiquewhite'=>'#FAEBD7','aquamarine'=>'#7FFFD4','beige'=>'#F5F5DC','black'=>'#000000','blue'=>'#0000FF','brown'=>'#A52A2A','cadetblue'=>'#5F9EA0','chocolate'=>'#D2691E','cornflowerblue'=>'#6495ED','crimson'=>'#DC143C','darkblue'=>'#00008B','darkgoldenrod'=>'#B8860B','darkgreen'=>'#006400','darkmagenta'=>'#8B008B','darkorange'=>'#FF8C00','darkred'=>'#8B0000','darkseagreen'=>'#8FBC8F','darkslategray'=>'#2F4F4F','darkviolet'=>'#9400D3','deepskyblue'=>'#00BFFF','dodgerblue'=>'#1E90FF','firebrick'=>'#B22222','forestgreen'=>'#228B22','gainsboro'=>'#DCDCDC','gold'=>'#FFD700','gray'=>'#808080','green'=>'#008000','greenyellow'=>'#ADFF2F','hotpink'=>'#FF69B4','indigo'=>'#4B0082','khaki'=>'#F0E68C','lavenderblush'=>'#FFF0F5','lemonchiffon'=>'#FFFACD','lightcoral'=>'#F08080','lightgoldenrodyellow'=>'#FAFAD2','lightgreen'=>'#90EE90','lightsalmon'=>'#FFA07A','lightskyblue'=>'#87CEFA','lightslategray'=>'#778899','lightyellow'=>'#FFFFE0','limegreen'=>'#32CD32','magenta'=>'#FF00FF','mediumaquamarine'=>'#66CDAA','mediumorchid'=>'#BA55D3','mediumseagreen'=>'#3CB371','mediumspringgreen'=>'#00FA9A','mediumvioletred'=>'#C71585','mintcream'=>'#F5FFFA','moccasin'=>'#FFE4B5','navy'=>'#000080','olive'=>'#808000','orange'=>'#FFA500','orchid'=>'#DA70D6','palegreen'=>'#98FB98','palevioletred'=>'#D87093','peachpuff'=>'#FFDAB9','pink'=>'#FFC0CB','powderblue'=>'#B0E0E6','red'=>'#FF0000','royalblue'=>'#4169E1','salmon'=>'#FA8072','seagreen'=>'#2E8B57','sienna'=>'#A0522D','skyblue'=>'#87CEEB','slategray'=>'#708090','springgreen'=>'#00FF7F','tan'=>'#D2B48C','thistle'=>'#D8BFD8','turquoise'=>'#40E0D0','violetred'=>'#D02090','white'=>'#FFFFFF','yellow'=>'#FFFF00');
  12. //http://www.w3schools.com/css/css_colornames.asp
  13. if ( ($color{0} != '#') and ( strstr($color,'(') === false ) ) $color = $common_colors[strtolower($color)];
  14. if ($color{0} == '#') //case of #nnnnnn or #nnn
  15. {
  16. $cor = strtoupper($color);
  17. if (strlen($cor) == 4) // Turn #RGB into #RRGGBB
  18. {
  19. $cor = "#" . $cor{1} . $cor{1} . $cor{2} . $cor{2} . $cor{3} . $cor{3};
  20. }
  21. $R = substr($cor, 1, 2);
  22. $vermelho = hexdec($R);
  23. $V = substr($cor, 3, 2);
  24. $verde = hexdec($V);
  25. $B = substr($cor, 5, 2);
  26. $azul = hexdec($B);
  27. $color = array();
  28. $color['R']=$vermelho;
  29. $color['G']=$verde;
  30. $color['B']=$azul;
  31. }
  32. else //case of RGB(r,g,b)
  33. {
  34. $color = str_replace("rgb(",'',$color); //remove ´rgb(´
  35. $color = str_replace("RGB(",'',$color); //remove ´RGB(´ -- PHP < 5 does not have str_ireplace
  36. $color = str_replace(")",'',$color); //remove ´)´
  37. $cores = explode(",", $color);
  38. $color = array();
  39. $color['R']=$cores[0];
  40. $color['G']=$cores[1];
  41. $color['B']=$cores[2];
  42. }
  43. if (empty($color)) return array('R'=>255,'G'=>255,'B'=>255);
  44. else return $color; // array['R']['G']['B']
  45. }
  46. function ConvertSize($size=5,$maxsize=0){
  47. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  48. //Identify size (remember: we are using 'mm' units here)
  49. if ( stristr($size,'px') ) $size *= 0.2645; //pixels
  50. elseif ( stristr($size,'cm') ) $size *= 10; //centimeters
  51. elseif ( stristr($size,'mm') ) $size += 0; //millimeters
  52. elseif ( stristr($size,'in') ) $size *= 25.4; //inches
  53. elseif ( stristr($size,'pc') ) $size *= 38.1/9; //PostScript picas
  54. elseif ( stristr($size,'pt') ) $size *= 25.4/72; //72dpi
  55. elseif ( stristr($size,'%') )
  56. {
  57. $size += 0; //make "90%" become simply "90"
  58. $size *= $maxsize/100;
  59. }
  60. else $size *= 0.2645; //nothing == px
  61. return $size;
  62. }
  63. function value_entity_decode($html)
  64. {
  65. //replace each value entity by its respective char
  66. preg_match_all('|&#(.*?);|',$html,$temparray);
  67. foreach($temparray[1] as $val) $html = str_replace("&#".$val.";",chr($val),$html);
  68. return $html;
  69. }
  70. function lesser_entity_decode($html)
  71. {
  72. //supports the most used entity codes
  73. $html = str_replace("&nbsp;"," ",$html);
  74. $html = str_replace("&amp;","&",$html);
  75. $html = str_replace("&lt;","<",$html);
  76. $html = str_replace("&gt;",">",$html);
  77. $html = str_replace("&laquo;","«",$html);
  78. $html = str_replace("&raquo;","»",$html);
  79. $html = str_replace("&para;","¶",$html);
  80. $html = str_replace("&euro;","€",$html);
  81. $html = str_replace("&trade;","™",$html);
  82. $html = str_replace("&copy;","©",$html);
  83. $html = str_replace("&reg;","®",$html);
  84. $html = str_replace("&plusmn;","±",$html);
  85. $html = str_replace("&tilde;","~",$html);
  86. $html = str_replace("&circ;","^",$html);
  87. $html = str_replace("&quot;",'"',$html);
  88. $html = str_replace("&permil;","‰",$html);
  89. $html = str_replace("&Dagger;","‡",$html);
  90. $html = str_replace("&dagger;","†",$html);
  91. return $html;
  92. }
  93. function AdjustHTML($html,$usepre=true)
  94. {
  95. //Try to make the html text more manageable (turning it into XHTML)
  96. //Remove javascript code from HTML (should not appear in the PDF file)
  97. $regexp = '|<script.*?</script>|si';
  98. $html = preg_replace($regexp,'',$html);
  99. $html = str_replace("\r\n","\n",$html); //replace carriagereturn-linefeed-combo by a simple linefeed
  100. $html = str_replace("\f",'',$html); //replace formfeed by nothing
  101. $html = str_replace("\r",'',$html); //replace carriage return by nothing
  102. if ($usepre) //used to keep \n on content inside <pre> and inside <textarea>
  103. {
  104. // Preserve '\n's in content between the tags <pre> and </pre>
  105. $regexp = '#<pre(.*?)>(.+?)</pre>#si';
  106. $thereispre = preg_match_all($regexp,$html,$temp);
  107. // Preserve '\n's in content between the tags <textarea> and </textarea>
  108. $regexp2 = '#<textarea(.*?)>(.+?)</textarea>#si';
  109. $thereistextarea = preg_match_all($regexp2,$html,$temp2);
  110. $html = str_replace("\n",' ',$html); //replace linefeed by spaces
  111. $html = str_replace("\t",' ',$html); //replace tabs by spaces
  112. $regexp3 = '#\s{2,}#s'; // turn 2+ consecutive spaces into one
  113. $html = preg_replace($regexp3,' ',$html);
  114. $iterator = 0;
  115. while($thereispre) //Recover <pre attributes>content</pre>
  116. {
  117. $temp[2][$iterator] = str_replace("\n","<br>",$temp[2][$iterator]);
  118. $html = preg_replace($regexp,'<erp'.$temp[1][$iterator].'>'.$temp[2][$iterator].'</erp>',$html,1);
  119. $thereispre--;
  120. $iterator++;
  121. }
  122. $iterator = 0;
  123. while($thereistextarea) //Recover <textarea attributes>content</textarea>
  124. {
  125. $temp2[2][$iterator] = str_replace(" ","&nbsp;",$temp2[2][$iterator]);
  126. $html = preg_replace($regexp2,'<aeratxet'.$temp2[1][$iterator].'>'.trim($temp2[2][$iterator]).'</aeratxet>',$html,1);
  127. $thereistextarea--;
  128. $iterator++;
  129. }
  130. //Restore original tag names
  131. $html = str_replace("<erp","<pre",$html);
  132. $html = str_replace("</erp>","</pre>",$html);
  133. $html = str_replace("<aeratxet","<textarea",$html);
  134. $html = str_replace("</aeratxet>","</textarea>",$html);
  135. // (the code above might slowdown overall performance?)
  136. } //end of if($usepre)
  137. else
  138. {
  139. $html = str_replace("\n",' ',$html); //replace linefeed by spaces
  140. $html = str_replace("\t",' ',$html); //replace tabs by spaces
  141. $regexp = '/\\s{2,}/s'; // turn 2+ consecutive spaces into one
  142. $html = preg_replace($regexp,' ',$html);
  143. }
  144. // remove redundant <br>'s before </div>, avoiding huge leaps between text blocks
  145. // such things appear on computer-generated HTML code
  146. $regexp = '/(<br[ \/]?[\/]?>)+?<\/div>/si'; //<?//fix PSPAD highlight bug
  147. $html = preg_replace($regexp,'</div>',$html);
  148. return $html;
  149. }
  150. function dec2alpha($valor,$toupper="true"){
  151. // returns a string from A-Z to AA-ZZ to AAA-ZZZ
  152. // OBS: A = 65 ASCII TABLE VALUE
  153. if (($valor < 1) || ($valor > 18278)) return "?"; //supports 'only' up to 18278
  154. $c1 = $c2 = $c3 = '';
  155. if ($valor > 702) // 3 letters (up to 18278)
  156. {
  157. $c1 = 65 + floor(($valor-703)/676);
  158. $c2 = 65 + floor((($valor-703)%676)/26);
  159. $c3 = 65 + floor((($valor-703)%676)%26);
  160. }
  161. elseif ($valor > 26) // 2 letters (up to 702)
  162. {
  163. $c1 = (64 + (int)(($valor-1) / 26));
  164. $c2 = (64 + (int)($valor % 26));
  165. if ($c2 == 64) $c2 += 26;
  166. }
  167. else // 1 letter (up to 26)
  168. {
  169. $c1 = (64 + $valor);
  170. }
  171. $alpha = chr($c1);
  172. if ($c2 != '') $alpha .= chr($c2);
  173. if ($c3 != '') $alpha .= chr($c3);
  174. if (!$toupper) $alpha = strtolower($alpha);
  175. return $alpha;
  176. }
  177. function dec2roman($valor,$toupper=true){
  178. //returns a string as a roman numeral
  179. if (($valor >= 5000) || ($valor < 1)) return "?"; //supports 'only' up to 4999
  180. $aux = (int)($valor/1000);
  181. if ($aux!==0)
  182. {
  183. $valor %= 1000;
  184. while($aux!==0)
  185. {
  186. $r1 .= "M";
  187. $aux--;
  188. }
  189. }
  190. $aux = (int)($valor/100);
  191. if ($aux!==0)
  192. {
  193. $valor %= 100;
  194. switch($aux){
  195. case 3: $r2="C";
  196. case 2: $r2.="C";
  197. case 1: $r2.="C"; break;
  198. case 9: $r2="CM"; break;
  199. case 8: $r2="C";
  200. case 7: $r2.="C";
  201. case 6: $r2.="C";
  202. case 5: $r2="D".$r2; break;
  203. case 4: $r2="CD"; break;
  204. default: break;
  205. }
  206. }
  207. $aux = (int)($valor/10);
  208. if ($aux!==0)
  209. {
  210. $valor %= 10;
  211. switch($aux){
  212. case 3: $r3="X";
  213. case 2: $r3.="X";
  214. case 1: $r3.="X"; break;
  215. case 9: $r3="XC"; break;
  216. case 8: $r3="X";
  217. case 7: $r3.="X";
  218. case 6: $r3.="X";
  219. case 5: $r3="L".$r3; break;
  220. case 4: $r3="XL"; break;
  221. default: break;
  222. }
  223. }
  224. switch($valor){
  225. case 3: $r4="I";
  226. case 2: $r4.="I";
  227. case 1: $r4.="I"; break;
  228. case 9: $r4="IX"; break;
  229. case 8: $r4="I";
  230. case 7: $r4.="I";
  231. case 6: $r4.="I";
  232. case 5: $r4="V".$r4; break;
  233. case 4: $r4="IV"; break;
  234. default: break;
  235. }
  236. $roman = $r1.$r2.$r3.$r4;
  237. if (!$toupper) $roman = strtolower($roman);
  238. return $roman;
  239. }
  240. ?>