如何使用文件输入在PDFJS中打开本地PDF? [英] how to open a local PDF in PDFJS using file input?

查看:1408
本文介绍了如何使用文件输入在PDFJS中打开本地PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法使用 input type =file选择pdf文件并使用 PDFJS

I would like to know if there is a way to select a pdf file using input type="file" and open it using PDFJS

推荐答案

你应该能够使用FileReader将文件对象的内容作为PDFJS接受的类型化数组获取( http://mozilla.github.io/pdf.js/api/draft/PDFJS.html

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)

//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);

 }

这篇关于如何使用文件输入在PDFJS中打开本地PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆