当前位置: 首页 > 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>

相关文章:

  • PDF.js无法显示数字签名
  • Leetcode-7 寻找用户推荐人
  • Electron桌面应用下,在拍照、展示pdf等模块时,容易导致应用白屏
  • 推荐一款PDF压缩的工具
  • 提取 PDF 文件中的文字以及图片中的文字
  • ​链表题解——回文链表【LeetCode】
  • 论文中pdf图片文件太大怎么办
  • 第2章_Excel_知识点笔记
  • Grafana-State timeline状态时间线
  • 从0开始学linux韦东山教程第四章问题小结(3)
  • 自然图像数据集
  • DeepSeek模型多模态扩展与跨模态理解实战
  • mybatis03
  • FastAPI安全认证:从密码到令牌的魔法之旅
  • 使用NMEA Tools生成GPS轨迹图
  • 第100期 DL,多输入多输出通道
  • [网页五子棋][匹配对战]落子实现思路、发送落子请求、处理落子响应
  • 论文略读:Auto-Regressive Moving Diffusion Models for Time Series Forecasting
  • 【nm】nm命令的使用:查看.so中的符号信息
  • RocketMQ介绍与部署
  • 网站管理手册/下载浏览器
  • wordpress 屏蔽国外ip/seo外链推广平台
  • 小说网站用什么虚拟主机/个人免费建站系统
  • python怎么做网站/如何利用互联网进行宣传推广
  • 手机移动端网站怎么做/如何制作网站教程
  • 做地方网站论坛/品牌推广策略有哪些