更新记录

1.0.1(2025-12-29)

  1. 重命名为 simple-pdf-viewer

1.0.0(2025-12-13)


平台兼容性

uni-app(4.87)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - × × × × 5.0 × ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
× × × × × × × × × - × ×

uni-app x(4.87)

Chrome Safari Android iOS 鸿蒙 微信小程序
× × 5.0 × × ×

其他

多语言 暗黑模式 宽屏模式 蒸汽模式
√ × √ √

simple-pdf-viewer - PDF预览插件

📖 简介

Android 原生 PDF 预览插件,提供流畅的应用内 PDF 预览体验。

注意: 此插件为自定义插件,不挂载到 uni 命名空间下,需要通过 @/uni_modules/simple-pdf-viewer 引入。

✨ 功能特性

  • ✅ 手势缩放
  • ✅ 页面旋转
  • ✅ 垂直翻页
  • ✅ 页码显示
  • ✅ 快速滑动
  • ✅ 页面对齐
  • ✅ TypeScript 支持

📱 平台支持

平台 支持情况
Android App ✅
iOS App ❌
H5 ❌
小程序 ❌

🚀 安装

此插件已内置在项目中,位于 src/uni_modules/simple-pdf-viewer 目录。

💡 使用方法

引入插件

import { previewPdf } from '@/uni_modules/simple-pdf-viewer'

基础用法

<template>
  <button @click="handlePreview">预览 PDF</button>
</template>

<script setup lang="ts">
import { previewPdf } from '@/uni_modules/simple-pdf-viewer'

function handlePreview() {
  previewPdf({
    filePath: '/storage/emulated/0/document.pdf',
    success: (res) => {
      console.log('预览成功')
    },
    fail: (err) => {
      uni.showToast({
        title: '预览失败: ' + err.errMsg,
        icon: 'none'
      })
    }
  })
}
</script>

预览网络 PDF

<template>
  <button @click="previewNetworkPdf">预览 PDF</button>
</template>

<script setup lang="ts">
import { previewPdf } from '@/uni_modules/simple-pdf-viewer'

async function previewNetworkPdf() {
  uni.showLoading({ title: '加载中...' })

  try {
    // 下载 PDF 文件
    const res = await new Promise<UniApp.DownloadSuccessData>((resolve, reject) => {
      uni.downloadFile({
        url: 'https://example.com/document.pdf',
        success: resolve,
        fail: reject
      })
    })

    // 转换为本地路径
    const filePath = plus.io.convertLocalFileSystemURL(res.tempFilePath)

    uni.hideLoading()

    // 预览 PDF
    previewPdf({
      filePath: filePath,
      fail: (err) => {
        uni.showToast({
          title: '预览失败: ' + err.errMsg,
          icon: 'none'
        })
      }
    })
  } catch (error) {
    uni.hideLoading()
    uni.showToast({ title: '下载失败', icon: 'none' })
  }
}
</script>

封装为工具函数

// utils/previewPdf.ts
import { previewPdf as simplePdfViewer } from '@/uni_modules/simple-pdf-viewer'

export async function previewPdf(filePathOrUrl: string) {
  let filePath = filePathOrUrl

  // 判断是否为网络 URL
  if (filePathOrUrl.startsWith('http://') || filePathOrUrl.startsWith('https://')) {
    uni.showLoading({ title: '加载中...' })

    try {
      const res = await new Promise<UniApp.DownloadSuccessData>((resolve, reject) => {
        uni.downloadFile({
          url: filePathOrUrl,
          success: resolve,
          fail: reject
        })
      })

      filePath = plus.io.convertLocalFileSystemURL(res.tempFilePath)
      uni.hideLoading()
    } catch (error) {
      uni.hideLoading()
      uni.showToast({ title: '下载失败', icon: 'none' })
      return
    }
  }

  simplePdfViewer({
    filePath: filePath,
    fail: (err) => {
      uni.showToast({
        title: `预览失败: ${err.errMsg}`,
        icon: 'none'
      })
    }
  })
}

使用:

import { previewPdf } from '@/utils/previewPdf'

// 预览网络 PDF
previewPdf('https://example.com/document.pdf')

// 预览本地 PDF
previewPdf('/storage/emulated/0/document.pdf')

📚 API

previewPdf(options)

import { previewPdf } from '@/uni_modules/simple-pdf-viewer'
参数 类型 必填 说明
options PreviewPdfOptions 是 预览配置

PreviewPdfOptions

属性 类型 必填 说明
filePath string 是 PDF 文件的本地路径
success Function 否 成功回调
fail Function 否 失败回调
complete Function 否 完成回调

错误码

错误码 说明
1400001 文件路径不能为空
1400002 找不到文件
1400003 不是有效的 PDF 文件
1400004 打开 PDF 失败

🎨 界面说明

  • 工具栏: 返回按钮、标题、旋转按钮
  • 页码显示: 底部显示当前页码和总页数
  • 手势支持: 滑动翻页、双指缩放、双击放大

⚠️ 注意事项

  1. 仅支持 Android 平台
  2. 需要读取存储权限
  3. 预览网络 PDF 需先下载到本地
  4. 使用 plus.io.convertLocalFileSystemURL() 转换下载文件路径

❓ 常见问题

Q: 为什么提示找不到文件? A: 请确保使用本地绝对路径,并用 plus.io.convertLocalFileSystemURL() 转换路径。

Q: 如何预览网络 PDF? A: 使用 uni.downloadFile 先下载到本地,再预览。参考上面的示例代码。

Q: PDF 加载慢怎么办? A: 添加加载提示,或压缩 PDF 文件大小。

📝 更新日志

查看 changelog.md

📄 许可证

本插件为付费插件,购买后可用于商业项目。

🤝 技术支持

  • DCloud 插件市场评论
  • 提交 Issue
  • 联系作者

开发者: Sanyi 最后更新: 2024-12-13

隐私、权限声明

1. 本插件需要申请的系统权限列表:

android.permission.READ_EXTERNAL_STORAGE,android.permission.INTERNET

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

无

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

无