我想知道是否有办法使用选择pdf文件input type="file"
并使用打开它PDFJS https://github.com/mozilla/pdf.js/
您应该能够使用 FileReader 来获取文件对象的内容作为类型化数组,pdfjs 接受该内容(https://mozilla.github.io/pdf.js/examples/ https://mozilla.github.io/pdf.js/examples/)
//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
const loadingTask = pdfjsLib.getDocument(typedarray);
loadingTask.promise.then(pdf => {
// The document is loaded here...
});
};
//Step 3:Read the file as ArrayBuffer
fileReader.readAsArrayBuffer(file);
}
编辑:自从我在 2015 年写下第一个答案以来,pdfjs API 在某个时候发生了变化。更新以反映截至 2021 年的新 API(感谢 @Chiel)以获取更新的答案
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)