从 arraybuffer 显示 pdf [英] Displaying pdf from arraybuffer

查看:32
本文介绍了从 arraybuffer 显示 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从此代码返回来自 laravel dompdf 的流数据

I am returning stream data from laravel dompdf from this code

 $pdf = App::make('dompdf.wrapper');
 $pdf->loadHTML("<div>This is test</div>");
 return $pdf->stream();

这是我的 JS ajax 代码

And this is my JS ajax code

    $.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);
    });

这是在ajax后显示pdf的HTML

Here is HTML to display pdf after ajax

<object id="pdfviewer" data="/files/sample.pdf" type="application/pdf" style="width:100%;height:500px;"></object>

我收到以下错误

无法加载 PDF 文档

Failed to load PDF document

请帮忙解决这个问题.如何显示pdf文件.

Please help to fix this. How to display pdf file.

推荐答案

jQuery.ajax() 默认没有 responseType 设置.您可以使用 polyfill,例如实现二进制数据的 jquery-ajax-blob-arraybuffer.js传输,或使用 fetch().

jQuery.ajax() does not have a responseType setting by default. You can use a polyfill, for example jquery-ajax-blob-arraybuffer.js which implements binary data transport, or utilize fetch().

另请注意,chrome、chromium 在 元素中显示 .pdf 时存在问题,请参阅 使用带有 blob URL 的对象嵌入标签显示 PDF, 使用 PDFObject 嵌入 Blob.使用