douzen1880 2016-01-28 12:11
浏览 31

使用PHP检索PDF(使用JSON)

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)