2 回答
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
TA贡献1844条经验 获得超8个赞
一种解决方案是不使用 PDF.js 并依赖浏览器处理 PDF 的open功能(如@MarcoDeveloper 建议的那样)。另一种解决方案是准备单独的页面,该页面将使用 PDF.js 显示您的文件。
为此,您需要准备简单的 html 页面。我建议使用帖子消息,但如果您的页面在同一域中,您也可以直接发送数据。
预览.html:
window.addEventListener("message", receiveMessage, false);
function receiveMessage(e) {
if (event.origin === window.location.origin) {
PDFJS.getDocument({ data: convertDataURIToBinary(e.data) }).then(function() {
});
}
}
索引.html:
var win = window.open('preview.html');
win.postMessage(dataURI, '*');
发送 base64 数据并在 preview.html 中解码可能是个好主意。
要直接发送 pdf 数据,您可以使用(仅在同一域中):
var win = window.open('preview.html');
win.PDFJS.getDocument({ data: binary_data });
文件 preview.html 只需要包含 PDF.js 的 JS 文件,它应该可以工作。
![?](http://img1.sycdn.imooc.com/5458655200013d9802200220-100-100.jpg)
TA贡献1856条经验 获得超17个赞
// ADD OPEN WHIT WINDOW JAVASCRIPT
var pdfData = convertDataURIToBinary('data:application/pdf;base64,' + data);
window.open(pdfData);
添加回答
举报