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');
// }
相关推荐
qqty121712 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
2401_8955213412 小时前
MySQL中between and的基本用法
android·数据库·mysql
l1t12 小时前
DeepSeek总结的用 C# 构建 DuckDB 插件说明
前端·数据库·c#·插件·duckdb
泯泷12 小时前
从零构建寄存器式 JSVMP:实战教程导读
前端·javascript·算法
开源盛世!!13 小时前
3.19-3.21
linux·服务器·前端
必胜刻13 小时前
AJAX 请求理解
前端·ajax·okhttp·前后端交互
云云鬼才13 小时前
CoCo编辑器、图形化编程怎么调用Scheme(跳转应用)
android
小民AI实战笔记13 小时前
NVM实战指南:高效管理你的Node.js环境
前端·node.js
www_stdio13 小时前
前端异步核心:Promise 从入门到吃透
前端
朱建伟13 小时前
大神尤雨溪再次出手,前端工具链整合--该文章是对vite plus官方README文档进行了翻译
前端·vite