4

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

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

请让我知道以上是否可以在 IE 11 中实现。

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

4

2 回答 2

3

此解决方案使用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

于 2016-12-19T04:05:22.110 回答
0

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

<iframe src="" name="printDoc" id="printDoc" width="800" height="1000" type="application/pdf"></iframe>

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

 $(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 性能的方法

于 2018-10-12T15:50:26.170 回答