1、使用pdf.js插件
需要在根目录下创建hybrid/html/pdf。详见:
build 和 web 文件可以拷贝
插件地址:http://mozilla.github.io/pdf.js/getting_started/
点击图片这个地方下载:

你会得到两个文件夹:

放到hybrid/html/pdf下面(没有需要自建)

2、创建一个page页面,用于作为容器,如下:
// /pages/showPdf/index
<template>
<view>
<web-view :src="url"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
url: "", // pdf链接
};
},
onLoad(options) {
let link = decodeURIComponent(options.link);
// 设置连接地址;
this.url = `/hybrid/html/pdf/web/viewer.html?file=${link}`;
},
};
</script>
3. 然后有需要预览pdf的页面,把pdf的地址传过来就可以了,如:(我这个page页面路径是/pages/showPdf/index)
goReport (url) {
let links = encodeURIComponent(url)
uni.navigateTo({
url: '/pages/showPdf/index?links=' + links,
})
},
4. 即可成功浏览,最后效果图如下:

网友评论