javascript - Using PHP to retrieve a PDF (with JSON) -
i'm using js , php collect rows of information mysql db. working fine until i'm adding code pdf-blob in same return.
var docs; getmini = function() { showminiloading(); var req = new xmlhttprequest(); req.onload = function() { console.log("got it!"); var temp = json.parse(this.responsetext); docs = temp; hideminiloading(); printallmini(); }; req.open("get", "resources/php/newmini.php", true); req.send(); console.log("sent!"); } showpdf = function(id) { (var = 0; < docs.length; i++) { var object = docs[i]; if (object.id == id) { console.log("found it! :d " + i); console.log("content: " + object.pdf); // more stuff here document.getelementbyid("pdf").innerhtml = '<object "data:application/pdf,' + object.pdf + '" type="application/pdf" width="100%" height="100%"> <p>alternative text - include link <a href="http://fzs.sve-mo.ba/sites/default/files/dokumenti-vijesti/sample.pdf">to pdf!</a></p> </object>'; break; } } }
<?php session_start(); include_once 'maindb.php'; $mysqli = mysqli_connect($dbmain['host'], $dbmain['user'], $dbmain['pass'], $dbmain['db']) or die('kunde inte ansluta till databasen:'.mysqli_error($maincon)); if(!$result = $mysqli->query( "select tbldokument.id, tblmail.inkommet, tbldokument.datum, tbldokument.moms, tbldokument.pris, tbldokument.org, tblverifikat.verifikatno, tbllevmallar.orgnr, tbllevmallar.name, tbldokumentsvg.svg, tbldokumentpdf.pdf tbldokument left outer join tblverifikat on tbldokument.id = tblverifikat.id left outer join tblmail on tbldokument.tblmail_id = tblmail.id left outer join tbllevmallar on tbldokument.orgnr = tbllevmallar.orgnr left outer join tbldokumentsvg on tbldokument.id = tbldokumentsvg.dokumentid left outer join tbldokumentpdf on tbldokument.id = tbldokumentpdf.id tblverifikat.verifikatno <> 'makulerad' order tbldokument.id")) { echo "verything bad"; } else { $i = 0; while ($row = $result->fetch_assoc()) { $temppdf = $row["pdf"]; $size = filesize($temppdf); header('content-type: application/pdf'); header("content-length: $size"); header('content-disposition: attachment; filename="new.pdf")'); $temparray[$i] = array( "id" => $row["id"], "arrived" => substr($row["inkommet"], 0, 10), "booked" => $row["datum"], "verification" => $row["verifikatno"], "org" => $row["name"], "price" => $row["pris"], "stax" => $row["moms"], "pic" => base64_encode($row["svg"]), "pdf" => $temppdf); $i++; } // header stuff $done = json_encode($temparray); $size = strlen($done); header('content-type: application/json'); header("content-length: $size"); header('connection: close'); echo $done; } $result->close(); $mysqli->close(); ?>
i encode result json @ end, whatever end column full of null-values, instead of desired pdf-blob. i've tried encode entire pdf base64_encode(), error says:
uncaught syntaxerror: unexpected token <
.. in console of browser.
actual question: how send pdf-blob other information , encoded in json?
i've tried lot of other threads haven't seen solution works in case :/
note: php , additional feedback efficiency of code above highly appreciated.
Comments
Post a Comment