MathJaxProcess.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. define('_MPDF_URI','../');
  3. define('_MPDF_PATH', '../');
  4. require_once __DIR__ . '/../vendor/autoload.php';
  5. $mpdf = new mPDF('');
  6. if (strpos($_REQUEST['bodydata'],'id%3D%22MathJax_SVG_Hidden%22')===false) {
  7. die("Hacking attempt");
  8. }
  9. $html = $_POST['bodydata'];
  10. $html = urldecode($html);
  11. preg_match('/<svg[^>]*>\s*(<defs.*?>.*?<\/defs>)\s*<\/svg>/',$html,$m);
  12. $defs = $m[1];
  13. $html = preg_replace('/<svg[^>]*>\s*<defs.*?<\/defs>\s*<\/svg>/','',$html);
  14. $html = preg_replace('/(<svg[^>]*>)/',"\\1".$defs,$html);
  15. preg_match_all('/<svg([^>]*)style="(.*?)"/',$html,$m);
  16. for ($i=0;$i<count($m[0]);$i++) {
  17. $style=$m[2][$i];
  18. preg_match('/width: (.*?);/',$style, $wr);
  19. $w = $mpdf->ConvertSize($wr[1],0,$mpdf->FontSize) * $mpdf->dpi/25.4;
  20. preg_match('/height: (.*?);/',$style, $hr);
  21. $h = $mpdf->ConvertSize($hr[1],0,$mpdf->FontSize) * $mpdf->dpi/25.4;
  22. $replace = '<svg'.$m[1][$i].' width="'.$w.'" height="'.$h.'" style="'.$m[2][$i].'"';
  23. $html = str_replace($m[0][$i],$replace,$html);
  24. }
  25. if ($_POST['PDF']=='PDF') {
  26. //=====================================================
  27. // ADD a stylesheet
  28. $stylesheet = '
  29. img { vertical-align: middle; }
  30. .MathJax_SVG_Display { padding: 1em 0; }
  31. #mpdf-create { display: none; }
  32. h3 {
  33. background-color: #EEEEEE;
  34. padding: 0.5em;
  35. border: 1px solid #8888FF;
  36. font-family: sans-serif;
  37. font-weight: bold;
  38. font-size: 14pt;
  39. }
  40. ';
  41. $mpdf->WriteHTML($stylesheet,1);
  42. $mpdf->WriteHTML($html);
  43. $mpdf->Output();
  44. //=====================================================
  45. }
  46. else {
  47. //=====================================================
  48. // To output SVG files readable by mPDF as text output
  49. header('Content-type: text/plain');
  50. preg_match_all('/<svg(.*?)<\/svg>/',$html,$m);
  51. for ($i=0;$i<count($m[0]);$i++) {
  52. $svg = $m[0][$i];
  53. $svg = preg_replace('/>/',">\n",$svg); // Just add some new lines
  54. echo $svg."\n\n";
  55. }
  56. //=====================================================
  57. }
  58. exit;