深入解析:预览pdf(url格式和blob格式)

news/2025/10/6 23:07:11/文章来源:https://www.cnblogs.com/tlnshuju/p/19128034

深入解析:预览pdf(url格式和blob格式)

2025-10-06 22:57  tlnshuju  阅读(0)  评论(0)    收藏  举报
<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
;
// 彻底清理前一个PDF
await
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;
// 成功加载后增加实例key
this.pdfInstanceKey++
;
}
catch (err) {
console.error('PDF加载失败:'
, err)
;
this.handleLoadError(err)
;
}
finally {
this.loading = false
;
}
}
,
createBlobUrl(fileObj
) {
// 释放之前的Blob URL
if (
this.currentBlobUrl) {
URL.revokeObjectURL(
this.currentBlobUrl)
;
}
this.currentBlobUrl = URL.createObjectURL(fileObj.originFileObj)
;
return
this.currentBlobUrl;
}
,
async cleanupPreviousPdf(
) {
// 清理加载任务
if (
this.activeLoadingTask) {
try {
// 先取消可能存在的promise
if (
this.activeLoadingTask._transport &&
this.activeLoadingTask._transport.destroy) {
this.activeLoadingTask._transport.destroy(
)
;
}
// 销毁worker
this.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.mzph.cn/news/929797.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

线性偏微分方程和非线性偏微分方程的区别

线性偏微分方程和非线性偏微分方程的区别区别的核心在于方程中未知函数及其各阶偏导数的“出现形式”。 参考:https://chat.deepseek.com/a/chat/s/e864c044-a73a-4271-8b7c-bb237c9f81d5

制作自己的网站需要什么材料如何做百度网站推广

在这之前&#xff0c;一直是在使用Scroll View进行滑动内容设置&#xff0c;但设置的都是不明不白的&#xff0c;而且有的时候设置好了之后也不知道是为什么&#xff0c;总感觉哪里不对劲&#xff0c;而且好也不知道为什么好&#xff0c;可能是长时间在做管理上的内容&#xff…

成都设计公司视频制作亚马逊关键词优化软件

【C】使用 list 与 string 实现基础字符串操作 文章目录 一、字符串的基础操作1.1 - startsWith1.2 - endsWith1.3 - trim1.4 - indexOf1.5 - replaceAll 二、list 基础操作2.1 - 遍历2.1.1 - 使用迭代器访问2.1.2 - 使用基于范围的 for 循环遍历2.1.3 - 使用标准算法库遍历 2.…

1-50 题解

1-10 1. A + B Problem #include <iostream> using namespace std;int main(){int a,b; cin>>a>>b;cout<<a+b<<endl;return 0; }2. Two Rectangles #include <iostream> using n…

基于AXI模块的视频流传输(ps控制篇)

在生成VDMA,GPIO,IIC后,会生成对于的bsp板级包。这里我不打算进行深入学习(不会尝试自己去写这份ps代码),要求能较为深入理解ps工作的原理即可,依旧不去细看HDMI模块相关。首先导入了几个库,然后定义了几个宏,…

lora的各种变体

lora的各种变体:lora+:对A(靠近输入端)和B(靠近输出端)设置不同的学习率以加速收敛,对B的学习率通常是A的4到16倍,这是因为近输出层的梯度更加稳定,近输入层的梯度相对不稳定,如果设置的学习率过大,容易梯度…

GO+RabbitMQ+Gin+Gorm+docker 部署 demo - 实践

GO+RabbitMQ+Gin+Gorm+docker 部署 demo - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &…

k8s pod启动失败困难排查

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Python测试

今日内容pycharm安装 git 版本控制 -- 协同开发 码云 -- 远程仓库 思维导图 个人博客 -- 技术人员昨日回顾typora 安装 使用标题 有序序号 无序序号 加粗 斜体 删除线 插入图片 插入表格 代码块、python的简介python2和…

免费文字转语音 AI 工具 All In One

免费文字转语音 AI 工具 All In One free TTS to Audio AI Tools TTSMaker 免费文字转语音免费文字转语音 AI 工具 All In Onefree TTS to Audio AI ToolsTTSMaker 免费文字转语音 每周限制 20000 个字符(部分声音可支…

Kubernetes Deployment:部署与管理应用指南

1. ReplicaSet(RS) 2. Deployment 3. Deployment资源配置 总结‍在上一章节中,介绍了pod,以及介绍了如何使用命令行来创建一个pod。那么问题来了,一般来说,我们部署微服务不可能只部署一个噻,肯定是部署多个,但…

wordpres做影视网站公关策划书模板范文

宏定义 不带参数 宏定义又称为宏代换、宏替换&#xff0c;简称“宏”。 格式&#xff1a; #define 标识符 字符串其中的标识符就是所谓的符号常量&#xff0c;也称为“宏名”。 预处理&#xff08;预编译&#xff09;工作也叫做宏展开&#xff1a;将宏名替换为字符串。 掌…

实用指南:Android高级开发第三篇 - JNI异常处理与线程安全编程

实用指南:Android高级开发第三篇 - JNI异常处理与线程安全编程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Co…

商城网站设计公司系统优化包括哪些

作者推荐 视频算法专题 本文涉及知识点 哈希映射 哈希集合 LeetCode 381. O(1) 时间插入、删除和获取随机元素 - 允许重复 RandomizedCollection 是一种包含数字集合(可能是重复的)的数据结构。它应该支持插入和删除特定元素&#xff0c;以及删除随机元素。 实现 Randomiz…

判断网站开发语言wordpress 访问速度

来源&#xff1a;德先生作者&#xff1a;朱圆恒&#xff0c;唐振韬&#xff0c;李伟凡&#xff0c;赵冬斌北京时间2019年1月25日2时&#xff0c;DeepMind在伦敦向世界展示了他们的最新成果——星际争霸2人工智能AlphaStar[1] 。图1. DeepMind AlphaStar挑战星际人类职业玩家直播…

分布式CAP理论 - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

【闲话】2025.9.24 记梦

破碎,离奇。话说 \(9.25\) 吃了 \(hz\) 校庆期间 \(hs\) 食堂免费饭菜的人第二天都被抓去做黑工,我和 @wkh2008 因为当时润去不知道干什么了所以没有被抓走,第二天回到班里发现毫无人,然后被学校里巡游的中介带走。…

北京撒网站设计git网站开发

文章目录 目录1. 程序的翻译环境和执行环境2. 详解编译链接2.1 翻译环境2.2 编译本身也分为几个阶段2.3 运行环境 3. 预处理详解3.1 预定义符号3.2 #define3.2.1 #define 定义标识符3.2.2 #define 定义宏3.2.3 #define 替换规则3.2.4 #和##3.2.5 带副作用的宏参数3.2.6 宏和函数…

完整教程:Postgresql常规SQL语句操作

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

酷派Cool20/20S/30/40手机安装Play商店-谷歌三件套-GMS方式

酷派Cool20/20S/30/40手机安装Play商店-谷歌三件套-GMS方式pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consol…