需求
首先,先请求后端接口,拿到pdf下载地址(若有跨越问题,请后端解决),通过uni.downloadFile
下载pdf模板,再使用uni.openDocument
新开页面打开文档进行预览(需装有可打开pdf的软件,若无,app、h5推荐使用h5内置预览pdf插件)。
代码如下
//url为pdf下载地址
toPreview(url) {
uni.downloadFile({
url,
success: (res) => {
if (res.statusCode == 200) {
console.log('下载成功', res);
let filePath = res.tempFilePath;
uni.openDocument({
filePath,
success: function(file) {
console.log('file-success',file);
}
});
}
}
})
},