我有一个 django 应用程序(我从中获取一些 html 作为客户端)和一个需要显示的 base64 编码的 PDF。我尝试了多种方法,它们在 Chrome/Firefox 中按预期工作。
我正在使用 django,所以会有一些模板和一些 JavaScript。pdf_preview_embed
是一个div
嵌入数据网址
<embed width=100% height=100% type="application/pdf" src="data:application/pdf;base64, {{ pdf }}"></embed>
Not Acceptable 解决方案,因为它可能需要内联 meg 数据。适用于 Windows 7 下的 IE11,不适用于 Windows 10 下的 Edge 和 IE11。嵌入 Blob
base64binary implementation
var blob = new Blob( [Base64Binary.decode(pdf)], {'type': 'application/pdf'} );
pdfURL = URL.createObjectURL( blob );
$('#pdf_preview_embed').html(
'<embed width=100% height=100% type="application/pdf" src="'+pdfURL+'"></embed>'
);
也不适用于 Edge 和 IE11。最佳答案
让 PDF.js 的 iframe 通过 iframe URI 加载 PDF 的 blob 的跨浏览器解决方法。
标准用例 blob URI 的示例:
/viewer.html?file=blob:19B579EA-5217-41C6-96E4-5D8DF5A5C70B
文件查看器.js:
在函数 webViewerOpenFileViaURL 中:
从以下更改行:
if (file && file.lastIndexOf('file:', 0) === 0) {
到:
if (file && file.lastIndexOf('file:', 0) === 0 || file && file.lastIndexOf('blob:', 0) === 0) {
并进一步阻止查看器在“原点”以 IE 11/Edge 方式运行时中断:
在函数 validateFileURL 中:
从以下更改行:
if (fileOrigin !== viewerOrigin) {
到:
if (fileOrigin != "null" && fileOrigin !== viewerOrigin) {
现在,FF、Chrome、IE 11 和 Edge 都在通过 URL 中的标准 blob URI 传递的 iframe 中的查看器中显示 PDF。
关于internet-explorer - 在 Edge/IE11 中显示 Blob PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505424/