I'm using JS and PHP to collect a rows of information from a MySQL DB. This is working fine until I'm adding code to get a PDF-blob in the 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) {
for (var i = 0; i < 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 a link <a href="http://fzs.sve-mo.ba/sites/default/files/dokumenti-vijesti/sample.pdf">to the 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
FROM 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
WHERE tblVerifikat.verifikatNo <> 'Makulerad'
ORDER BY tblDokument.ID"))
{
echo "VERYTHING IS 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 the result with JSON at the end, but whatever I do I end up with a column full of null-values, instead of the desired PDF-blob. I've also tried to encode the entire pdf with base64_encode(), but then I get an error that says:
Uncaught SyntaxError: Unexpected token <
.. in the console of my browser.
Actual question: How do i send a PDF-blob together with some other information and encoded in JSON?
I've tried a lot of other threads but haven't seen a solution that works in this case :/
NOTE: I am very now to PHP and any additional feedback about the efficiency of the code above is highly appreciated.
</div>