<?php
  // pdf2json_test.php - RJM Programming - Use the great MacPorts pdf2json and then display resultant Json back here in HTML (if valid Json, that is)
?>
<html>
<head>
<style>
 body { font-size: 14pt; }
</style>
<?php
echo '<script type="text/javascript">' . "\n";
if (isset($_GET['injsonfile'])) {
  if (str_replace(".PDF", "", $_GET['injsonfile']) != $_GET['injsonfile'] && file_exists($_GET['injsonfile'])) {
    exec("pdf2json " . $_GET['injsonfile'] . " " . str_replace(".PDF", ".json", $_GET['injsonfile']));
    echo "var x1 = 2; \n";
    echo "var c1 = '" . $_GET['injsonfile']  . "'; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents(str_replace(".PDF", ".json", $_GET['injsonfile']))) . "'; \n";
  } else if (str_replace(".pdf", "", $_GET['injsonfile']) != $_GET['injsonfile'] && file_exists(dirname(__FILE__) . "/" . $_GET['injsonfile'])) {
    echo "var a1 = '" . "pdf2json " . dirname(__FILE__) . "/" . $_GET['injsonfile'] . " " . str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile'])  . "'; \n";
    exec("pdf2json " . dirname(__FILE__) . "/" . $_GET['injsonfile'] . " " . str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile']));
    echo "var x1 = 3; \n";
    echo "var c1 = '" . str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile'])  . "'; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents(str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile']))) . "'; \n";
  } else if (str_replace(".pdf", "", $_GET['injsonfile']) != $_GET['injsonfile'] && file_exists($_GET['injsonfile'])) {
    exec("pdf2json " . $_GET['injsonfile'] . " " . str_replace(".pdf", ".json", $_GET['injsonfile']));
    echo "var x1 = 1; \n";
    echo "var c1 = '" . $_GET['injsonfile']  . "'; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents(str_replace(".pdf", ".json", $_GET['injsonfile']))) . "'; \n";
  } else if (str_replace(".PDF", "", $_GET['injsonfile']) != $_GET['injsonfile'] && file_exists($_GET['injsonfile'])) {
    exec("pdf2json " . $_GET['injsonfile'] . " " . str_replace(".PDF", ".json", $_GET['injsonfile']));
    echo "var x1 = 2; \n";
    echo "var c1 = '" . $_GET['injsonfile']  . "'; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents(str_replace(".PDF", ".json", $_GET['injsonfile']))) . "'; \n";
  } else if (str_replace(".pdf", "", $_GET['injsonfile']) != $_GET['injsonfile'] && file_exists(dirname(__FILE__) . "/" . $_GET['injsonfile'])) {
    exec("pdf2json " . dirname(__FILE__) . "/" . $_GET['injsonfile'] . " " . str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile']));
    echo "var x1 = 3; \n";
    echo "var c1 = '" . str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile'])  . "'; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents(str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile']))) . "'; \n";
  } else if (str_replace(".PDF", "", $_GET['injsonfile']) != $_GET['injsonfile'] && file_exists(dirname(__FILE__) . "/" . $_GET['injsonfile'])) {
    exec("pdf2json " . dirname(__FILE__) . "/" . $_GET['injsonfile'] . " " . str_replace(".PDF", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile']));
    echo "var x1 = 4; \n";
    echo "var c1 = '" . str_replace(".PDF", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile'])  . "'; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents(str_replace(".pdf", ".json", dirname(__FILE__) . "/" . $_GET['injsonfile']))) . "'; \n";
  } else { 
    echo "var x1 = 5; \n";
    echo "var text = '" . str_replace("'", "&#39;", file_get_contents($_GET['injsonfile'])) . "'; \n";
  }
} else {
  echo "var x1 = 6; \n";
  echo "var text = '" . str_replace("'", "&#39;", file_get_contents("http://www.rjmprogramming.com.au/PHP/pdf2json_test/cover_letter_template_3.json")) . "'; \n";
  //echo "var text = '" . str_replace("'", "&#39;", file_get_contents("http://localhost:8888/Acknowledgements.json")) . "'; \n";
}
?>
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

  x1 = x1;

}else{

  alert("Json is not ok");

}
<?php
echo "var obj = JSON.parse(text); \n";
echo "var sobj = '" . dirname(__FILE__) . "'; \n";
?>

var biggesttop = 0;

function process() {
 var i = 0, j = 0, k, dis = '', t, h, w, l, f, ff, ss, nbt = biggesttop;
 while (j >= 0) {
  try {
   //alert(i + ":" + j);
   dis = obj[i].text[j].data;
   //alert(dis);
   //if (dis.replace("Copyright","") != dis && i > 0) {
   //  alert(i + ": " + j + "; " + dis + obj[i].text[j].top + " (" + biggesttop + "-" + nbt + ")");
   //}
   //if (dis.replace("URI","") != dis && i > 0) {
   //  alert(i + ": " + j + "; " + dis + obj[i].text[j].top + " (" + biggesttop + "-" + nbt + ")");
   //}
   dis = dis.replace("actionURI:", "");
   l = obj[i].text[j].left;
   t = eval(obj[i].text[j].top + biggesttop);
   if (eval(t) > eval(nbt)) nbt = t;
   w = obj[i].text[j].width;
   h = obj[i].text[j].height;
   f = obj[i].text[j].font;
   if (f == 0) {
     document.write('<p style="position:absolute;left:' + l + ';top:' + t + ';">' + dis + '</p>');
   } else {
     //if (dis.replace("Copyright","") != dis && i > 0) {
     //  alert(f);
     //}
     ff = obj[0].fonts[eval(f - 1)].family;
     //if (ff == "Times") ff = "'Times New Roman', Times, serif";
     ss = obj[0].fonts[eval(f - 1)].size;
     //if (dis.replace("Copyright","") != dis && i > 0) {
     //  alert(i + ": " + j + "; " + dis + obj[i].text[j].top + " (" + biggesttop + "-" + nbt + ")");
     //}
     document.write('<p style="position:absolute;left:' + l + ';top:' + t + ';font-family:' + ff + ';font-size:' + eval((ss - 1) * 100.0 / 14.0) + '%;">' + dis + '</p>');
   }
   j = eval(j + 1);
   //if (dis.replace("Copyright","") != dis && i > 0) {
   //  alert(i + ": " + j + "; " + dis + obj[i].text[j].top + " (" + biggesttop + "-" + nbt + ")");
   //}
  } catch (eee) {
   //alert(95);
   i = eval(i + 1);
   j = 0;
   try {
    dis = obj[i].text[j].data;
    biggesttop = nbt;
    //document.write('<hr style="position:absolute;top:' + biggesttop + ';">');
   } catch (ee) {
    //alert(92);
    j = -1;
   }
  }
 }
}

</script>
</head>
<body onload='process(); '>
<p id="demo"></p>
</body>
</html>
