关于javascript:js-pdf预览

4次阅读

共计 534 个字符,预计需要花费 2 分钟才能阅读完成。

url 预览

function previewPdf(url, filename) {window.open(url, filename, `width=1060,height=${screen.height},left=${(screen.width - 1060)>>1}`)
}

文件流 预览

function objectURL2Blob(url) {
    return new Promise(resolve => {let xhr = new XMLHttpRequest()
        xhr.open('GET', url, true)
        xhr.responseType = 'blob'
        xhr.onload = function() {if (this.status == 200) resolve(this.response)
        }
        xhr.send()})
}

let file = null // 文件
let url = URL.createObjectURL(file)
let blob = new Blob([await objectURL2Blob(url)], {type: 'application/pdf'})
url = URL.createObjectURL(blob)
previewPdf(url, 'JavaScript 权威指南.pdf')

局部浏览器可能不反对


正文完
 0