![](/img/trans.png)
[英]PDF.js: using postMessage to pass base64 to viewer.html in iframe
[英]how to open base64 String response from server in external viewer (viewer.html) using pdf.js?
在我的應用程序中,我正在向服務器發送路徑,並且該路徑返回了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;
}
我現在得到的輸出:
我已經回答了一些問題,但無法解決。 所以請幫幫我。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.