javascript 如何使用文件输入在 PDFJS 中打开本地 PDF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22048395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:17:22 来源:igfitidea点击:
how to open a local PDF in PDFJS using file input?
提问by user2422940
回答by Sam
You should be able to use a FileReader to get the contents of a file object as a typed array, which PDFJS accepts (http://mozilla.github.io/pdf.js/api/draft/PDFJS.html)
您应该能够使用 FileReader 将文件对象的内容作为类型化数组获取,PDFJS 接受(http://mozilla.github.io/pdf.js/api/draft/PDFJS.html)
//Step 1: Get the file from the input element
inputElement.onchange = function(event) {
var file = event.target.files[0];
//Step 2: Read the file using file reader
var fileReader = new FileReader();
fileReader.onload = function() {
//Step 4:turn array buffer into typed array
var typedarray = new Uint8Array(this.result);
//Step 5:PDFJS should be able to read this
PDFJS.getDocument(typedarray).then(function(pdf) {
// do stuff
});
};
//Step 3:Read the file as ArrayBuffer
fileReader.readAsArrayBuffer(file);
}