- //一个是通过第三方软件查看pdf
- uni.downloadFile({ //通过uniapp的api下载下来
- url: 'test.pdf',
- success: function (res) {
- var filePath = res.tempFilePath;
- uni.openDocument({
- filePath: filePath,
- success: function (FileRes) {
- console.log('打开文档成功');
- }
- });
- }
- });
- //另一种是通过pdf.js在uniapp中查看的
- //新建webView.vue
- <template>
- <view style="width: 100%;" >
- <web-view :src="allUrl"></web-view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- src: '',
- allUrl:'',
- viewerUrl: '/hybrid/html/web/viewer.html', // 格式化文件流的一个js 文件 文末会分享出来
- }
- },
- onLoad(options) {
- let fileUrl = encodeURIComponent("pdf的地址") // encodeURIComponent 函数可把字符串作为 URI 组件进行编码。
- this.allUrl = this.viewerUrl + '?file=' + fileUrl
- }
- }
- </script>
- //////////////////////////////////////////////////
- //然后引入文件夹hybrid放在项目根目录下
- //然后就传路径到webView
- //列如
- uni.navigateTo({
- url:'../filePreview?url='+你的pdf路径
- })