- 下载pdf.js官网地址
- 将pdf.js文件包中的web和build复制到项目文件(/hybrid/html/)下
- 新建一个vue文件,如file-preview.vue,添加以下代码
<template>
<view>
<web-view :style="{'height':windowHeight}" style="width: 100%;" :src="allUrl"></web-view>
</view>
</template>
<script>
// import globalConfig from '@/config'
export default {
data() {
return {
// pdf.js中的build和web文件存储在该项目的/hybrid/html/路径下
viewerUrl: '/hybrid/html/web/viewer.html',
allUrl:'',
windowHeight: "200px"
}
},
onLoad(option) {
uni.getSystemInfo({
success: (res) => {
this.windowHeight = (res.windowHeight-10)+"px";
} })
let url=encodeURIComponent(option.url)
this.allUrl=this.viewerUrl+'?file='+url;
}
}
</script>
其中 option为其他页面传过来的参数, option.url为文档流路径,例如:url=‘http://58.49.74.231:85/UploadFile/Get?fileName=%E6%B1%87%E6%8A%A5%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_S_20191101170956271.pdf’
4.如果文件流的域名和pdf.js的域名不相同,会报错,需要在web/viewer.js中吧跨域报错的信息注释掉
如果想要想要进行优化,可以看看这篇文章(在掘金看到的)如何实现高性能的在线 PDF 预览