更新记录
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 | × | × | × |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| √ | × | √ |
uni-pdf-viewer - PDF预览插件
📖 简介
Android 原生 PDF 预览插件,提供流畅的应用内 PDF 预览体验。
✨ 功能特性
- ✅ 手势缩放
- ✅ 页面旋转
- ✅ 垂直翻页
- ✅ 页码显示
- ✅ 快速滑动
- ✅ 页面对齐
- ✅ TypeScript 支持
📱 平台支持
| 平台 | 支持情况 |
|---|---|
| Android App | ✅ |
| iOS App | ❌ |
| H5 | ❌ |
| 小程序 | ❌ |
🚀 安装
- 在 DCloud 插件市场 搜索
uni-pdf-viewer - 点击「使用 HBuilderX 导入插件」
- 选择云端一体方式导入到项目
💡 使用方法
基础用法
<template>
<button @click="handlePreview">预览 PDF</button>
</template>
<script setup lang="ts">
function handlePreview() {
uni.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">
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
uni.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
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
}
}
uni.previewPdf({
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
uni.previewPdf(options)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| options | PreviewPdfOptions | 是 | 预览配置 |
PreviewPdfOptions
| 属性 | 类型 | 必填 | 说明 |
|---|---|---|---|
| filePath | string | 是 | PDF 文件的本地路径 |
| success | Function | 否 | 成功回调 |
| fail | Function | 否 | 失败回调 |
| complete | Function | 否 | 完成回调 |
错误码
| 错误码 | 说明 |
|---|---|
| 1400001 | 文件路径不能为空 |
| 1400002 | 找不到文件 |
| 1400003 | 不是有效的 PDF 文件 |
| 1400004 | 打开 PDF 失败 |
🎨 界面说明
- 工具栏: 返回按钮、标题、旋转按钮
- 页码显示: 底部显示当前页码和总页数
- 手势支持: 滑动翻页、双指缩放、双击放大
⚠️ 注意事项
- 仅支持 Android 平台
- 需要读取存储权限
- 预览网络 PDF 需先下载到本地
- 使用
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

收藏人数:
购买源码授权版(
试用
赞赏(0)
下载 2
赞赏 0
下载 12176799
赞赏 1828
赞赏
京公网安备:11010802035340号