vue或uniapp使用pdf.js预览

一、先下载稳定版的pdf.js,可以去官网下载 官网下载地址pdf.js包下载(已配置好,无需修改)

二、下载好的pdf.js文件放在public下静态文件里, uniapp是放在 static下静态文件里

三、使用方式

1. vue项目 注意路径 :src="`static/pdfjs-1.9/web/viewer.html?file=你的pdf路径
html 复制代码
<iframe :src="`static/pdfjs-1.9/web/viewer.html?file=https://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf`" style="border:none;" width="1000" height="100%"></iframe>
2. uniapp项目使用 注意路径 /static/PDFVIEW/pdfjs-1.9/web/viewer.html
html 复制代码
<template>
    <view style="width: 100vh;">
        <web-view :src="pdfUrl" :fullscreen="true"></web-view>
    </view>
</template>
<script>
export default {
    data() {
        return {
            pdfUrl: '',
            htmlUrl: '/static/PDFVIEW/pdfjs-1.9/web/viewer.html', //新测试预览
     fileUrl:'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf',  
        }
    },
    onLoad(options) {
        this.pdfUrl = `${this.htmlUrl}?file=${this.fileUrl}`;
    }
}
</script>

四、控制台会出现跨域问题,这时候要注释跨域代码,

在viewer.js文件找到这段代码,(在1861行)然后注释掉
javascript 复制代码
 var fileOrigin = new URL(file, window.location.href).origin;
 if (fileOrigin !== viewerOrigin) {
   throw new Error('file origin does not match viewer\'s');
 }

五、这时候基本就能看了


pc端效果

手机端效果

相关推荐
宁雨桥9 小时前
深入理解 async/await的原理
前端·javascript·es8
心连欣9 小时前
轮播图实战:用Web API玩转元素属性操作
前端·javascript·api
剑神一笑10 小时前
从 JSON.parse 到树形视图:实现一个在线 JSON 格式化工具
前端·javascript·json
淸湫10 小时前
JavaScript常用正则表达式大全
前端·javascript
脱缰胖虎10 小时前
vue3+lodash+ts+tailwin 实现多行文本的展开收起代码(支持渲染html)
前端·vue.js
米丘11 小时前
vue3.5.x 单文件组件(SFC)样式编译过程
css·vue.js·postcss
最炫的美少女战士11 小时前
claude code 安装报错claude native binary not installed.
javascript
肥羊zzz11 小时前
Vue2 vs Vue3 中 v-for 的 key 用法对比
前端·vue.js
用户23678298016811 小时前
Canvas:实现一个高颜值二维码生成器
javascript
剑神一笑12 小时前
从字符串到时间线:实现一个 Cron 表达式解析器
javascript·typescript·reactjs