在我的应用程序中,我发送一条到服务器的路径,并返回一个以base64字符串形式的pdf文件内容。现在我想使用pdf.js在外部查看器(viewer.html)中显示此字符串。我正在以下面的方式做这件事,我做错了什么?
var res="JVBERi0xLjUK..."; // Base64 String from response shortened
var pdfData = base64ToUint8Array(res);
window.open('pdfJs/viewer.html?file='+pdfData);
function base64ToUint8Array(base64) {
var raw = atob(base64); //This is a native function that decodes a base64-encoded string.
var uint8Array = new Uint8Array(new ArrayBuffer(raw.length));
for(var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
我现在得到的输出:
我已经完成了一些答案,但无法解决这个问题。所以请帮助我。
答案 0 :(得分:4)
Uint8Array无法通过查询字符串传递。在打开的窗口上使用PDFViewerApplication.open()方法,例如
var w = window.open('web/viewer.html?file=');
w.addEventListener('load', function () {
var res="JVBERi0xLjUK..."; // Base64 String from response shortened
var pdfData = base64ToUint8Array(res);
w.PDFViewerApplication.open(pdfData);
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for(var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
}, true);