uniapp + vue2 + pfdjs + web-view 实现安卓、iOS App PDF预览

文章目录

概要

在使用uniapp开发安卓、iOS APP的时候遇到一个需求,预览PDF,特此记录

  1. 下载pdfjs 解压到static文件夹中 pdfjs
  2. githup地址

技术栈

uniapp + vue2 + pfdjs + webview

完整代码

javascript 复制代码
<template>
  <view class="pdf-module" style="width: 100%; height: 100vh">
    <web-view :src="allUrl" :webview-styles="webviewStyles"></web-view>
  </view>
</template>
<script>
export default {
  data() {
    return {
      allUrl: '',
      viewerUrl: '/static/pdf/web/viewer.html',
      webviewStyles: {
        progress: { color: '#007aff' }
      }
    }
  },
  onLoad(option) {
    const encodedPdfUrl = encodeURIComponent(option.url)
    const baseUrl = this.getBaseUrl()
    this.allUrl = `${baseUrl}${this.viewerUrl}?file=${encodedPdfUrl}`
  },
  methods: {
    getBaseUrl() {
      const platform = uni.getSystemInfoSync().platform
      return platform === 'ios' ? '/' : ''
    }
  }
}
</script>
<style scoped>
.pdf-module {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
</style>

效果图

小结

javascript 复制代码
注释 / 删除 同源校验逻辑
// 原错误代码(大概1802行)
if (fileOrigin !== viewerOrigin) {
  throw new Error('file origin does not match viewer\'s');
}

// 修改后(注释掉校验)
// if (fileOrigin !== viewerOrigin) {
//   throw new Error('file origin does not match viewer\'s');
// }
相关推荐
恋猫de小郭25 分钟前
移动端开发稳了?AI 目前还无法取代客户端开发,小红书的论文告诉你数据
前端·flutter·ai编程
文心快码BaiduComate43 分钟前
百度云与光本位签署战略合作:用AI Agent 重构芯片研发流程
前端·人工智能·架构
闲云一鹤1 小时前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
QCY2 小时前
「完全理解」1 分钟实现自己的 Coding Agent
前端·agent·claude
一拳不是超人2 小时前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
anyup2 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
雮尘3 小时前
如何在非 Claude IDE (TARE、 Cursor、Antigravity 等)下使用 Agent Skills
前端·agent·ai编程
icebreaker3 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker3 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
wuhen_n3 小时前
代码生成:从AST到render函数
前端·javascript·vue.js