当前位置: 首页 > 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.xdnf.cn/news/783469.html

相关文章:

  • 【C/C++】初步了解享元模式
  • Linux账号和权限管理
  • ubuntu 20.04挂载固态硬盘
  • 什么是“音节”?——语言构成的节拍单位
  • 【Java Web】7.事务管理AOP
  • 【Spring】Spring哪些源码解决了哪些问题P1
  • WINUI——Magewell视频捕捉开发手记
  • “packageManager“: “pnpm@9.6.0“ 配置如何正确启动项目?
  • Vue-ref 与 props
  • 不止抓请求:5种开发场景中的抓包组合策略(含 Charles 等工具)
  • SpringBoot 数据库导入导出 Xlsx文件的导入与导出 全量导出 数据库导出表格 数据处理 外部数据
  • DHCP 动态主机配置协议(Dynamic host configuration protocol)逐层封装过程: DHCP --> UDP --> IP
  • 从0到1,带你走进Flink的世界
  • iOS 电子书听书功能的实现
  • [yolov11改进系列]基于yolov11使用FasterNet替换backbone用于轻量化网络的python源码+训练源码
  • React---day8
  • Express 集成Sequelize+Sqlite3 默认开启WAL 进程间通信 Conf 打包成可执行 exe 文件
  • mobilnet v4 部署笔记
  • [yolov11改进系列]基于yolov11引入自集成注意力机制SEAM解决遮挡问题的python源码+训练源码
  • 机器学习实战36-基于遗传算法的水泵调度优化项目研究与代码实现
  • 华为云Flexus+DeepSeek征文|基于华为云Flexus X实例的小说转语音助手应用构建实录
  • java int 颜色值转换为string 不带透明度
  • Numpy入门2——视图和副本、伪随机数、切片和索引、数组的轴操作
  • 从0到1认识EFK
  • Oracle 用户/权限/角色管理
  • Linux随记(十八)
  • ElasticSearch+Gin+Gorm简单示例
  • cacti导出的1分钟监控数据csv文件读取并按5分钟求平均值,然后计算95计费值,假设31天的月份
  • graphviz, dot, Error: lost rA sA edge; 独立的模块
  • 项目执行中缺乏灵活应对机制,如何增强适应性?