PDFObject 在浏览器中显示不正确
PDFObject is not showing correctly in the browser
我正在尝试使用 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
你知道怎么解决吗?
它似乎以二进制格式出现,因此您需要再次将其转换为 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();
请尝试以上方法。
我发现问题是pdf文件的编码问题,所以我在后端使用'base64-stream':
将文件编码为base64
var readStream = fs.createReadStream(uploadFileFd);
// This will wait until we know the readable stream is actually valid before piping
readStream.on('open', function () {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(new Base64Encode()).pipe(res);
});
// This catches any errors that happen while creating the readable stream (usually invalid names)
readStream.on('error', function(err) {
res.end(err);
});
之后我使用嵌入标签来显示 pdf:
var newElement = "<embed src=data:application/pdf;base64,"+data+" id='pdf' width='100%' height='1200'>";
我正在尝试使用 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
你知道怎么解决吗?
它似乎以二进制格式出现,因此您需要再次将其转换为 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();
请尝试以上方法。
我发现问题是pdf文件的编码问题,所以我在后端使用'base64-stream':
将文件编码为base64 var readStream = fs.createReadStream(uploadFileFd);
// This will wait until we know the readable stream is actually valid before piping
readStream.on('open', function () {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(new Base64Encode()).pipe(res);
});
// This catches any errors that happen while creating the readable stream (usually invalid names)
readStream.on('error', function(err) {
res.end(err);
});
之后我使用嵌入标签来显示 pdf:
var newElement = "<embed src=data:application/pdf;base64,"+data+" id='pdf' width='100%' height='1200'>";