尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

深入解析:预览pdf(url格式和blob格式)
📅 发布时间:2026/6/20 6:05:13

深入解析:预览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>

相关新闻

  • 线性偏微分方程和非线性偏微分方程的区别
  • selenium基础 - 教程
  • 详细介绍:20250602在荣品的PRO-RK3566开发板的Android13下的uboot启动阶段配置BOOTDELAY为10s

最新新闻

  • 揭秘AI教材编写:低查重AI工具助力,快速产出优质教材!
  • 仿真时序精度陷阱:从timescale作用域到跨模块参数传递的实战解析
  • 从数据手册到实战:MAX31856热电偶测温芯片全解析
  • 2026年荆门市贵金属旧料回收优质靠谱实体门店精选五家 黄金回收铂金回收白银回收彩金回收真实探店测评清单及联系方式推荐 - 前途无量YY
  • 2026年荆州市贵金属旧料回收优质靠谱实体门店精选五家 黄金回收铂金回收白银回收彩金回收真实探店测评清单及联系方式推荐 - 前途无量YY
  • 「指南」从零到一:Conda环境管理与实战避坑

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号