uniapp之安卓端pdf预览
原理:
将文件下载到本地,使用uni api预览文件
exportPDF() {
uni.downloadFile({
url: "http://192.168.1.237:9000/profile/statute/937820.pdf",
success: res => {
console.log(res)
if (res.statusCode === 200) {
// 保存pdf文件至手机,一般安卓端存储路径为:手机存储/dcim/camera文件夹下
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function() {
uni.showToast({
title: "文件已保存至/DCIM/CAMERA文件夹下",
icon: "none"
})
setTimeout(function() {
// 预览pdf文件
uni.openDocument({
filePath: res.tempFilePath,
showMenu: true,
success: function(file) {
console.log("file-success",
file)
}
})
}, 1500)
},
fail: function() {
uni.showToast({
title: "保存失败,请稍后重试!",
icon: "none"
})
}
})
}
}
})
}
直接调用即可。
注释:测试过pdfjs-3.5.141-dist、pdfjs-2.16.105-dist可在浏览器正常使用,真机可以看到页面打不开pdf文件。
钻研不易,转载请注明出处。。。。。。