H5中预览接口返回的pdf文件流

引入pdf.js插件,可以完美解决pdf预览的兼容性问题。

第一步:

打开网址:PDF.js - Getting Started

拿到下载之后的安装包,先解压。

第二步:

在项目的static文件夹中新建一个目录pdf,将解压后的安装包文件复制到这里

第三步:

调获取pdf文件流的接口,在请求中携带文件id等信息,设置请求返回类型为文件流(responseType: 'arraybuffer'),拿到接口返回的文件流。

第四步:

将文件流转换为url地址,拼接在/static/pdf/web/viewer.html路径后面

this.pdfSrc = `${window.location.origin}/static/pdf/web/viewer.html?file=${encodeURIComponent(fileUrl)}`

const fileId = '08dd5bb4-b0ca-4d29-835c-7dcc7802a695'; // 假设是你从后端获取的文件 ID
				const requestUrl = `你的接口地址/${fileId}`;
				uni.request({
					url: requestUrl, // 接口地址
					method: 'GET',
					responseType: 'arraybuffer', // 设置返回类型为文件流
					success: (res) => {
						if (res.statusCode === 200) {
							// 手动将 ArrayBuffer 转换为 Blob 对象
							const fileBlob = new Blob([res.data], {
								type: 'application/pdf'
							}); // 根据返回文件的实际类型,调整 MIME 类型

							// 转换为 URL
							const fileUrl = URL.createObjectURL(fileBlob);
							this.pdfSrc =
								`${window.location.origin}/static/pdf/web/viewer.html?file=${encodeURIComponent(fileUrl)}`
							console.log(fileUrl, this.pdfSrc, '这是文件地址--------');
						} else {
							uni.showToast({
								title: '获取文件失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '请求失败',
							icon: 'none'
						});
					}
				});
			

第五步: 

使用 iframe 显示 PDF 文件
        <iframe v-if="pdfSrc" :src="pdfSrc" style="width: 100%; height: 100vh;" frameborder="0"></iframe>

<!-- 使用 iframe 显示 PDF 文件 -->
		<iframe v-if="pdfSrc" :src="pdfSrc" style="width: 100%; height: 100vh;" frameborder="0"></iframe>

在 Vue3 中,如果你想让用户在 H5 页面上预览上传的 PDF 文件,你可以使用一些前端库来实现这个功能,例如 pdf.js 或者 html2canvas。下面是简单的步骤: 1. 首先,在项目中安装需要的库: ``` npm install @vueuse/core pdfjs-dist html2canvas --save ``` 2. 引入依赖并创建一个可以预览 PDF 的组件,比如 `PdfPreview.vue`: ```html <template> <div> <button @click="previewFile">预览PDF</button> <iframe v-if="previewIframe" :src="pdfUrl" style="display:none;"></iframe> </div> </template> <script> import { useAsync } from '@vueuse/core'; import * as PDFJS from 'pdfjs-dist'; export default { data() { return { previewIframe: false, pdfUrl: '' }; }, methods: { async previewFile(file) { if (!file.type.includes('application/pdf')) { alert('请选择PDF文件'); return; } const url = URL.createObjectURL(file); this.pdfUrl = url; try { await PDFJS.getDocument(url).promise.then((doc) => { // 初始化预览组件,显示第一页 this.previewIframe = true; this.$nextTick(() => { const firstPage = doc.getPage(1); firstPage.render({ canvasContext: document.getElementById('preview-canvas'), viewport: firstPage.getViewport({ scale: 1 }), }); }); }); } finally { URL.revokeObjectURL(url); // 关闭URL引用,释放资源 } }, }, }; </script> ``` 3. 在模板中添加一个 canvas 元素用于渲染 PDF 内容: ```html <template> ... <canvas id="preview-canvas"></canvas> </template> ``` 这样,用户点击预览按钮后,会选择并预览他们上传的PDF文件。记得处理可能出现的错误,并在不需要预览时隐藏 iframe。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值