UniApp_小程序 实现pdf文件预览

一、样式

暂无

二、使用
export function previewFile(url) {
	// url: 文件全路径
    // #ifdef MP-WEIXIN
    previewWechat(url)
    // #endif

    // #ifdef H5
    previewH5(url)
    // #endif
}

export function previewWechat(urlPdf) {
    uni.showLoading({
        title: '正在加载中..'
    })
    uni.downloadFile({
        url: urlPdf,
        success: function(res) {
            var filePath = res.tempFilePath;
            uni.openDocument({
                filePath: filePath,
                showMenu: true,
                success: function(res) {
                    uni.hideLoading()
                },
            });
        },
        complete: function(r) {
            uni.hideLoading()
        }
    });
}

export function previewH5(url) {
    uni.getSystemInfo({
        success: function(res) {
            switch (res.platform) {
                case "android":
                    console.log("platform: android")
                    uni.navigateTo({
                        url: '/pages/task/file-preview?fileUrl=' + encodeURI(url) +
                            "&isPdfView=true",
                    })
                    break;
                case "ios":
                    console.log("platform: ios")
                    uni.navigateTo({
                        url: '/pages/task/file-preview?fileUrl=' + encodeURI(url),
                    })
                    break;
                default:
                    console.log('platform: ', res.platform)
                    uni.navigateTo({
                        url: '/pages/task/file-preview?fileUrl=' + encodeURI(url),
                    })
                    break;
            }
        },
    })
}
三、file-preview 组件
<template>
    <view class="file-pre">
        <web-view :src="fileUrl"></web-view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                fileUrl: "",
                pdfViewUrl: '/static/pdfview/web/viewer.html'
            }
        },
        onLoad(options) {
            this.fileUrl = decodeURI(options.fileUrl)
            if (!!options.isPdfView) {
                this.fileUrl = this.pdfViewUrl + '?file=' + encodeURI(this.fileUrl)
            }
        },
    }
</script>

<style lang="scss"></style>
四、插件pdfview,位于 /static/pdfview 处
### 如何在 UniApp 小程序实现 PDF 文件预览 #### 使用 WebView 组件加载在线 PDF 文件 为了实现UniApp 微信小程序预览 PDF 文件的功能,可以利用 `WebView` 组件来加载外部 URL 或者通过 base64 编码的方式展示本地文件。对于网络上的 PDF 文档可以直接指定其链接作为 web-view 的 src 属性值[^1]。 ```html <template> <view class="container"> <!-- 加载远程PDF --> <web-view :src="pdfUrl"></web-view> </view> </template> <script> export default { data() { return { pdfUrl: 'https://example.com/sample.pdf' }; } }; </script> ``` #### 处理本地存储的 PDF 文件 如果需要显示来自应用内部资源或者是用户上传到设备中的 PDF,则先要将其转换成适合 web-view 显示的形式,比如转为 Base64 字符串再传给组件[^2]。 ```javascript // 假设已经有一个 File 对象 fileObj 表示选中的 PDF 文件 const reader = new FileReader(); reader.onloadend = function () { const base64String = this.result.split(',')[1]; that.setData({ pdfBase64: `data:application/pdf;base64,${base64String}` }); }; reader.readAsDataURL(fileObj); ``` 之后就可以像之前那样把得到的 base64 数据赋值给 `<web-view>` 的 `src` 属性了: ```html <web-view :src="pdfBase64"></web-view> ``` #### 解决跨平台兼容性问题 考虑到不同操作系统和浏览器之间的差异,在实际项目里可能还会遇到一些特定环境下的 bug 和异常情况。因此建议开发者们仔细测试各个目标平台上该功能的表现,并针对发现的问题做出相应调整优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值