PDFObject在浏览器中无法正确显示

问题描述 投票:0回答:1

我正在尝试使用PDFObject渲染嵌入的pdf文件。在后端,我发送pdf如下

fs.readFile(uploadFileFd, function (err,data){
    res.contentType("application/pdf");
    res.send(data);
 });

之后,我会在前面得到如下响应

$.get("/loadDocument",function(data){
    PDFObject.embed(data,"#test");
  });

而且我得到以下结果image with the render in the browser of the pdf

您知道如何解决此问题吗?

javascript html sails.js pdfobject
1个回答
0
投票

似乎是二进制格式,因此您需要再次将其转换为pdf以便在浏览器上呈现。

var request = new XMLHttpRequest();
request.open("GET", "/path/to/pdf", true); 
request.responseType = "blob";
request.onload = function (e) {
    if (this.status === 200) {
        // `blob` response
        console.log(this.response);
        // create `objectURL` of `this.response` : `.pdf` as `Blob`
        var file = window.URL.createObjectURL(this.response);
        var a = document.createElement("a");
        a.href = file;
        a.download = this.response.name || "detailPDF";
        document.body.appendChild(a);
        a.click();
        // remove `a` following `Save As` dialog, 
        // `window` regains `focus`
        window.onfocus = function () {                     
          document.body.removeChild(a)
        }
    };
};
request.send();

请尝试以上操作。

© www.soinside.com 2019 - 2024. All rights reserved.