当前位置: 首页 > news >正文

预览pdf(url格式和blob格式)

<template><div class="pdf-container"><div v-if="loading" class="loading-state"><a-spin size="large" /></div><div v-else-if="error" class="loading-state">加载失败,请关闭弹窗重新加载!</div><div v-else class="pdf-viewer"><pdfv-for="i in numPages":key="`${pdfInstanceKey}-${i}`":src="pdfInstance":page="i"class="pdf-page"/></div></div>
</template><script>
import pdf from 'vue-pdf';
import { debounce } from 'lodash-es';export default {name: "PdfViewer",components: { pdf },props: {currentPdfUrl: { type: [String, Object], required: true },fileType: { type: Number, default: 1 }},data() {return {numPages: 0,pdfInstance: null,pdfInstanceKey: 0,loading: false,error: false,activeLoadingTask: null,currentBlobUrl: null};},watch: {currentPdfUrl: {immediate: true,deep: true,handler: debounce(function(newVal) {if (newVal) this.loadPdf(newVal);}, 300)}},methods: {async loadPdf(source) {try {this.loading = true;this.error = false;// 彻底清理前一个PDFawait this.cleanupPreviousPdf();// 准备新的PDF源const pdfSource = this.fileType === 1? { url: source, withCredentials: false }: this.createBlobUrl(source);// 创建加载任务this.activeLoadingTask = this.fileType === 1? pdf.createLoadingTask({url: source,withCredentials: false,cMapUrl: '\'@/assets/cmaps/\'',// 'https://fastly.jsdelivr.net/npm/pdfjs-dist@2.11.338/cmaps/',cMapPacked: true}): pdf.createLoadingTask(this.createBlobUrl(source));this.pdfInstance = this.activeLoadingTask;// 监听加载完成const pdfDocument = await this.activeLoadingTask.promise;this.numPages = pdfDocument.numPages;// 成功加载后增加实例keythis.pdfInstanceKey++;} catch (err) {console.error('PDF加载失败:', err);this.handleLoadError(err);} finally {this.loading = false;}},createBlobUrl(fileObj) {// 释放之前的Blob URLif (this.currentBlobUrl) {URL.revokeObjectURL(this.currentBlobUrl);}this.currentBlobUrl = URL.createObjectURL(fileObj.originFileObj);return this.currentBlobUrl;},async cleanupPreviousPdf() {// 清理加载任务if (this.activeLoadingTask) {try {// 先取消可能存在的promiseif (this.activeLoadingTask._transport &&this.activeLoadingTask._transport.destroy) {this.activeLoadingTask._transport.destroy();}// 销毁workerthis.activeLoadingTask.destroy();} catch (e) {console.warn('清理PDF worker时出错:', e);}this.activeLoadingTask = null;}// 重置状态this.pdfInstance = null;this.numPages = 0;},handleLoadError(error) {this.error = true;this.numPages = 0;// 特殊处理常见错误if (error.name === 'PasswordException') {console.warn('PDF需要密码');} else if (error.name === 'InvalidPDFException') {console.warn('无效的PDF文件');}},retryLoading() {this.loadPdf(this.currentPdfUrl).catch(()=>{});}},beforeDestroy() {this.cleanupPreviousPdf();if (this.currentBlobUrl) {URL.revokeObjectURL(this.currentBlobUrl);}}
};
</script><style scoped lang="less">
.pdf-container {width: 100%;//height: 100%;overflow: auto;background-color: #f0f0f0;.pdf-viewer {display: flex;flex-direction: column;align-items: center;.pdf-page {margin-bottom: 20px;box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);background-color: white;width: 100%;&:last-child {margin-bottom: 0;}}}
}
.loading-state{display: flex;justify-content: center;align-items: center;height: 100%;
}
</style>
http://www.lqws.cn/news/106093.html

相关文章:

  • Python Day42
  • xmake的简易学习
  • 一、无参数的函数调用- RSP,EAX寄存器,全局变量,INT类型和MOV,INC,SHL指令
  • Python中os模块详解
  • Spring Boot 自动配置原理:从入门到精通
  • webstrom中git插件勾选提交部分文件时却出现提交全部问题怎么解决
  • UGUI Text/TextMeshPro字体组件
  • Activity
  • 6.3本日总结
  • agent mode 代理模式,整体要求,系统要求, 系统指令
  • ABP-Book Store Application中文讲解 - Part 7: Authors: Database Integration
  • 『uniapp』把接口的内容下载为txt本地保存 / 读取本地保存的txt文件内容(详细图文注释)
  • WPS word 已有多级列表序号
  • 免费批量文件重命名软件
  • AI健康小屋+微高压氧舱:科技如何重构我们的健康防线?
  • KITTI数据集(计算机视觉和自动驾驶领域)
  • mobilnet v4 部署笔记
  • go语言基础|slice入门
  • C语言学习—数据类型20250603
  • 2025.6.3总结
  • Jpom:Java开发者的一站式自动化运维平台详解
  • Java编程之建造者模式
  • 深度学习入门Day2--鱼书学习(1)
  • 【Typst】4.导入、包含和读取
  • Spring AI Alibaba + Nacos 动态 MCP Server 代理方案
  • Playwright定位器详解:自动化测试的核心工具
  • 集合类基础概念
  • 2023年12月四级真题作文的分析总结
  • 704. 二分查找 (力扣)
  • 十五、【测试执行篇】异步与并发:使用 Celery 实现测试任务的后台执行与结果回调