【问题标题】:Display embedded PDF in Internet Explorer 11 from binary string or base64在 Internet Explorer 11 中从二进制字符串或 base64 显示嵌入的 PDF
【发布时间】:2017-03-03 09:29:11
【问题描述】:

有一个 3rd 方服务向我发送二进制字符串或 base64 编码的 PDF 文件。是否有可能使用二进制字符串或 base64 编码显示嵌入在 IE 11 中的 PDF。

从 SO 和其他论坛中,我得出结论,IE 11 仅支持图像的数据 uri,而不支持排除 base64 的 PDF(我可能错了)。所以剩下的唯一选择是从二进制字符串显示。我在 Node 应用程序中使用它,但我没有选择先将检索到的文件保存到 Node 服务器并使用静态 URL。

如果以上在 IE 11 中可以实现,请告诉我。

目前我正在尝试使用 https://github.com/pipwerks/PDFObject 的 npm 包。对于 Chrome 和 Firefox,我检索了 base64 文件并使用上述包嵌入它并且工作正常。

【问题讨论】:

  • 您找到解决方案了吗?
  • @JDS 请参考我的回答。
  • @QuangMinhLENGUYEN 是的,我的朋友。但是,在您今天针对您提到的重复问题发布答案之前,该答案在 4 个月前并不存在。虽然,请注意你的答案仍然没有像这个问题的原始问题那样在 IE 中显示嵌入的 PDF
  • @roray 使用我的解决方案,当单击“下载”按钮时,浏览器会要求您从 base64 字符串打开或下载 PDF 文件。允许这样做的代码行是:window.navigator.msSaveOrOpenBlob(blob, fileName);您可以通过更改数据变量在 IE 浏览器中从 jsfiddle 测试我的代码:PDF 文件 base64 字符串。

标签: javascript html internet-explorer-11 pdf.js pdfobject


【解决方案1】:

此解决方案使用pdf.js

使用 PDF.js 库渲染 base64 PDF 的关键步骤

  • 先用 atob 解码
  • 然后使用上面的解码数据初始化一个 Uint8Array

来自 express-pdfjs/scripts/App.js 的摘要

let options = {
      method: 'GET',
      uri: 'http://localhost:5000/getBase64Pdf',
      resolveWithFullResponse: true
    }
rp(options)
  .then((response) => {
    if (response.statusCode !== 200) {
      console.error('http not 200 but : ', response.statusCode)
    } else {
      console.info('connected successfully : ' + response.statusCode)
      let pdfData = atob(response.body)
      let uint8ArrayPdf = new Uint8Array(pdfData.length)
      for (let i = 0; i < pdfData.length; i++) {
        uint8ArrayPdf[i] = pdfData.charCodeAt(i)
      }
      let pdfjsframe = document.getElementById('pdfViewer');
      pdfjsframe.contentWindow.PDFViewerApplication.open(uint8ArrayPdf);
    }
  })

pdfViewer 是 index.html 中的 iframe

<iframe id="pdfViewer" src="http://localhost:3000/express-pdfjs/pdfViewer/web/viewer.html" height="1600" width="850" />

请在客户端使用 React 找到一个示例实现 边 @https://github.com/rohanray/so-pdf-base64

更新:还可以看看@user3590235 答案:Display embedded PDF in Internet Explorer 11 from binary string or base64

【讨论】:

  • 此方法 (....PDFViewerApplication.open(uint8ArrayPdf) ) 在 IE 11/Edge 中是否有效(受支持)?
  • @user3590235 它肯定适用于 IE 11,因为此解决方案是 IE 11 专门需要的。您可以尝试。我目前没有要在 IE 11 中测试的 Windows 机器
  • 谢谢@roray。我实际上继续尝试。它在 IE 11 和 Edge 上都有效,但它太慢了,我正试图找到解决这个问题的方法。
  • @user3590235 数组操作可能会占用大量 CPU。文件的大概大小是多少?您可以尝试在 service worker 上运行上述数组操作。
  • @roray 非常感谢您的提示。它是美联储。今天放假,所以今天无法访问该文档,但显然它足够大,足以导致问题(在 IE 中 - Chrome 剂量很快)。我明天去办公室时会使用Service Worker。
【解决方案2】:

在与@roray 讨论之后 - 我在这里的菜单上添加了一个稍微不同的解决方案 :) 首先:1.不为此使用base64字符串(尽管可能)。 2.我正在研究asp mvc 3.使用pdf.js的viewer.html查看器

所以,从服务器/控制器从 db 中获取文件并返回位

public ActionResult LoadFile(int id)
{
   var file = db.Files.Where(i => i.Id == id).FirstOrDefault();
   return File(file.BinaryFile, MediaTypeNames.Application.Pdf, "Name.pdf");
}

在视图/html 中添加源未指定 (src="") 的 iframe 或者可以在页面加载事件中动态创建它。顺便说一句,我尝试使用 FF/Chrome/Edge/IE 进行对象、嵌入和 iframe 11 并发现 iframe 似乎始终如一地工作。虽然不喜欢 iframe:/

&lt;iframe src="" name="printDoc" id="printDoc" width="800" height="1000" type="application/pdf"&gt;&lt;/iframe&gt;

最后,在您的脚本标签中。这同样可以通过 Ajax 完成。基本上,一旦文档准备好调用服务器,以字节为单位获取文件,转换为 blob url,将 blob url 附加到 '/web/viewer.html?file=' 的相对位置您的项目并更新 iframe 的 src="" 属性。

 $(document).ready(function myfunction() {
 var xhr = new XMLHttpRequest();
    // works for EDGE/Chrome/FFox
    xhr.open("GET", "/Documents/LoadFile/" + fileId, true);
    xhr.responseType = "blob";
    xhr.onload = function (e) {
        if (this.status === 200) {
            var blob = new Blob([this.response], { type: 'application/pdf' });
            console.log('Not IE 11');
            var url = window.URL.createObjectURL(blob);
            _iFrame = document.querySelector("#printDoc");
            _iFrame.setAttribute('src', '/web/viewer.html?file=' + url);
        }
    };
    xhr.send();
    
});

这将打开嵌入在 iframe 中的 viewer.html 中的 pdf。 MS Edge 可以正常工作,但不能通过地址栏作为查询参数发送 blob url,这显然受 FF/Chrome 支持。而且,这就是我选择此选项的原因。

PS。 IE 11 对我来说仍然没有希望,所以我使用了 var blob = new Blob([this.response], {type: 'application/pdf' });window.navigator.msSaveOrOpenBlob(blob, fileName);如果你找到请告诉我一种提高 IE 11 性能的方法

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 1970-01-01
    • 2011-12-10
    • 2016-09-09
    • 1970-01-01
    • 2017-07-23
    • 2011-09-28
    • 2020-03-30
    • 2014-11-27
    相关资源
    最近更新 更多