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端效果

手机端效果

相关推荐
李明卫杭州2 小时前
在 JavaScript 中,生成器函数(Generator Function)
前端·javascript
qq8406122333 小时前
Nodejs+vue基于elasticsearch的高校科研期刊信息管理系统_mb8od
前端·vue.js·elasticsearch
Quz4 小时前
QML与JavaScript 交互的四种方式
javascript·qt·交互
会周易的程序员4 小时前
cNetgate插件架构设计详解 动态库 脚本二开lua, python, javascript
javascript·c++·python·物联网·lua·iot
NEXT067 小时前
普通函数与箭头函数的区别
前端·javascript·面试
TechFind8 小时前
如何为 AI Agent 写出完美的 SOUL.md 人格文件(2026指南)
javascript
薛一半8 小时前
React三大属性之refs
前端·javascript·react.js
Lao乾妈官方认证唯一女友:D8 小时前
Ethers.js使用方法
javascript·web3
程序员林北北8 小时前
【前端进阶之旅】Svelte:编译即框架,让前端开发回归简洁
前端·javascript·vue.js·react.js·html5
echoVic9 小时前
给 Agent Skill 装上「黑匣子」:STOP 可观测性协议设计与实现
java·javascript