laravel 从 arraybuffer 显示 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42106584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying pdf from arraybuffer
提问by GRESPL Nagpur
I am returning stream data from laravel dompdf from this code
我正在从此代码返回来自 laravel dompdf 的流数据
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML("<div>This is test</div>");
return $pdf->stream();
And this is my JS ajax code
这是我的 JS ajax 代码
$.ajax({
type:"GET",
url: "/display",
responseType: 'arraybuffer'
}).done(function( response ) {
var blob = new Blob([response.data], {type: 'application/pdf'});
var pdfurl = window.URL.createObjectURL(blob)+"#view=FitW";
$("#pdfviewer").attr("data",pdfurl);
});
Here is HTML to display pdf after ajax
这是在 ajax 后显示 pdf 的 HTML
<object id="pdfviewer" data="/files/sample.pdf" type="application/pdf" style="width:100%;height:500px;"></object>
I am getting below error
我得到以下错误
Failed to load PDF document
无法加载 PDF 文档
Please help to fix this. How to display pdf file.
请帮助解决这个问题。如何显示pdf文件。
采纳答案by guest271314
jQuery.ajax()
does not have a responseType
setting by default. You can use a polyfill, for example jquery-ajax-blob-arraybuffer.jswhich implements binary data transport, or utilize fetch()
.
jQuery.ajax()
responseType
默认情况下没有设置。您可以使用polyfill,例如实现二进制数据传输的jquery-ajax-blob-arraybuffer.js,或利用fetch()
.
Note also, chrome, chromium have issues displaying .pdf
at either <object>
and <embed>
elements, see Displaying PDF using object embed tag with blob URL, Embed a Blob using PDFObject. Substitute using <iframe>
element for <object>
element.
还要注意,铬,铬有显示问题.pdf
在任<object>
和<embed>
元素,请参阅使用对象嵌入标记与斑点URL显示PDF,嵌入使用PDFObject的Blob。用<iframe>
元素替换<object>
元素。
$(function() {
var pdfsrc = "/display";
var jQueryAjaxBlobArrayBuffer = "https://gist.githubusercontent.com/SaneMethod/"
+ "7548768/raw/ae22b1fa2e6f56ae6c87ad0d7fbae8fd511e781f/"
+ "jquery-ajax-blob-arraybuffer.js";
var script = $("<script>");
$.get(jQueryAjaxBlobArrayBuffer)
.then(function(data) {
script.text(data).appendTo("body")
}, function(err) {
console.log(err);
})
.then(function() {
$.ajax({
url: pdfsrc,
dataType: "arraybuffer"
})
.then(function(data) {
// do stuff with `data`
console.log(data, data instanceof ArrayBuffer);
$("#pdfviewer").attr("src", URL.createObjectURL(new Blob([data], {
type: "application/pdf"
})))
}, function(err) {
console.log(err);
});
});
});
Using fetch()
, .arrayBuffer()
使用fetch()
,.arrayBuffer()
var pdfsrc = "/display";
fetch(pdfsrc)
.then(function(response) {
return response.arrayBuffer()
})
.then(function(data) {
// do stuff with `data`
console.log(data, data instanceof ArrayBuffer);
$("#pdfviewer").attr("src", URL.createObjectURL(new Blob([data], {
type: "application/pdf"
})))
}, function(err) {
console.log(err);
});
plnkr http://plnkr.co/edit/9R5WcsMSWQaTbgNdY3RJ?p=preview
plnkr http://plnkr.co/edit/9R5WcsMSWQaTbgNdY3RJ?p=preview
version 1 jquery-ajax-blob-arraybuffer.js
, jQuery.ajax()
; version 2 fetch()
, .arrayBuffer()
版本 1 jquery-ajax-blob-arraybuffer.js
, jQuery.ajax()
; 版本 2 fetch()
,.arrayBuffer()
回答by jobB
I like guest271314answer a lot, especially the second version using fetch, but I wanted to add a solution that does not use a polyfill or an experimental technology like fetch
.
我很喜欢guest271314的答案,尤其是使用fetch的第二个版本,但我想添加一个不使用polyfill或实验技术的解决方案,如fetch
.
This solution uses the native XMLHttpRequest APIto create the request. This allows us to change the responseType
to arrayBuffer
.
此解决方案使用本机XMLHttpRequest API来创建请求。这允许我们将 更改responseType
为arrayBuffer
。
var xhr = new XMLHttpRequest();
var pdfsrc = "https://upload.wikimedia.org/wikipedia/en/6/62/Definition_of_management.pdf";
xhr.open('GET', pdfsrc, true);
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function (evt) {
var data = evt.target.response;
if (this.status === 200) {
$("#pdfviewer").attr("src", URL.createObjectURL(new Blob([data], {
type: "application/pdf"
})));
}
}, false);
xhr.send();
I forked guest271314s plnkr to show this method in action: http://plnkr.co/edit/7tfBYQQdnih9cW98HSXX?p=preview
我分叉了guest271314s plnkr来展示这个方法:http://plnkr.co/edit/7tfBYQQdnih9cW98HSXX?p=preview
回答by Andrew Monks
From my tests the responce is in response not response.data, so the following should work:
从我的测试中,响应是响应而不是 response.data,所以以下应该有效:
$.ajax({
type:"GET",
url: "/display",
responseType: 'arraybuffer'
}).done(function( response ) {
var blob = new Blob([response], {type: 'application/pdf'});
var pdfurl = window.URL.createObjectURL(blob)+"#view=FitW";
$("#pdfviewer").attr("data",pdfurl);
});
Although it seems JQuery is doing something with the responce causing a blank PDF output... (PDF is blank when downloading using javascript). This will work:
虽然看起来 JQuery 正在对导致空白 PDF 输出的响应做一些事情......(使用 javascript 下载时 PDF 是空白的)。这将起作用:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'test.pdf', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (this.status == 200) {
var blob=new Blob([this.response], {type:"application/pdf"});
var pdfurl = window.URL.createObjectURL(blob)+"#view=FitW";
$("#pdfviewer").attr("data",pdfurl);
}
};
xhr.send();