PdfViewer.vue
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!--
* @Date: 2025-01-21
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-10-21 14:33:07
* @FilePath: /mlaj/src/components/ui/PdfViewer.vue
* @Description: PDF预览组件 - 使用pdf-vue3库
-->
<template>
<van-popup v-if="show" :show="show" @update:show="emit('update:show', $event)" position="right"
:style="{ height: '100%', width: '100%' }">
<div class="pdf-viewer-container">
<!-- PDF内容区域 -->
<div class="pdf-content" :class="{ 'pdf-no-select': preventSave }">
<PDF v-if="url && show && !loadingError" :src="url" :showProgress="true" :progressColor="'#1989fa'"
:showPageTooltip="true" :showBackToTopBtn="true" :scrollThreshold="300" :pdfWidth="'100%'"
:rowGap="8" :page="1" :cMapUrl="'https://unpkg.com/pdfjs-dist@3.7.107/cmaps/'"
:withCredentials="false" :useSystemFonts="true" :stopAtErrors="false" :disableFontFace="false"
:disableRange="false" :disableStream="false" :disableAutoFetch="false" @onProgress="handleProgress"
@onComplete="handleComplete" @onScroll="handleScroll" @onPageChange="handlePageChange"
@onPdfInit="handlePdfInit" @onError="handlePdfError" />
<!-- 错误状态显示 -->
<div v-if="loadingError" class="error-container">
<div class="error-content">
<van-icon name="warning-o" size="48" color="#ff4444" />
<div class="error-title">加载失败</div>
<div class="error-message">{{ errorMessage }}</div>
<van-button type="primary" size="small" @click="retryLoad" class="retry-btn">
重新加载
</van-button>
</div>
</div>
</div>
<!-- 关闭按钮 -->
<van-button class="close-btn" type="default" icon="cross" round @click="emit('update:show', false)" />
<!-- 加载遮罩 -->
<van-overlay :show="loading && !loadingError">
<div class="wrapper" @click.stop>
<div class="loading-content">
<!-- 进度环形图 -->
<div class="progress-circle">
<div class="progress-percent">{{ loadingProgress }}%</div>
</div>
<!-- 加载文字 -->
<div class="loading-text">
<div class="loading-title">正在加载PDF文档</div>
<div class="loading-subtitle">
{{ loadingProgress < 10 ? '正在连接服务器...' : loadingProgress < 50 ? '正在下载文件...' :
loadingProgress < 90 ? '正在解析文档...' : '即将完成...' }} </div>
</div>
</div>
</div>
</van-overlay>
</div>
</van-popup>
</template>
<script setup>
import { ref, watch, nextTick } from 'vue';
import PDF from "pdf-vue3";
/**
* 组件属性定义
*/
const props = defineProps({
// 是否显示弹窗
show: {
type: Boolean,
default: false
},
// PDF文件URL
url: {
type: String,
default: ''
},
// PDF标题
title: {
type: String,
default: ''
},
// 是否防止保存(禁用右键、长按等)
preventSave: {
type: Boolean,
default: true
}
});
/**
* 事件定义
*/
const emit = defineEmits(['update:show', 'onLoad', 'onProgress', 'onComplete']);
// 响应式数据
const loading = ref(true);
const loadingProgress = ref(0);
const loadingError = ref(false);
const errorMessage = ref('');
/**
* 监听show属性变化,控制PDF加载
*/
watch(() => props.show, (newVal) => {
if (newVal && props.url) {
// 重置所有状态
loading.value = true;
loadingError.value = false;
loadingProgress.value = 0;
errorMessage.value = '';
} else if (!newVal) {
// 弹窗关闭时重置状态
loading.value = true;
loadingError.value = false;
loadingProgress.value = 0;
errorMessage.value = '';
}
});
/**
* 监听URL变化,重新加载PDF
*/
watch(() => props.url, (newUrl) => {
if (newUrl && props.show) {
// URL变化时重置状态
loading.value = true;
loadingError.value = false;
loadingProgress.value = 0;
errorMessage.value = '';
}
});
/**
* 处理PDF下载进度
* @param {number} loadRatio - 加载进度 0-100
*/
const handleProgress = (loadRatio) => {
loadingProgress.value = Math.round(loadRatio);
emit('onProgress', loadRatio);
};
/**
* 处理PDF下载完成
*/
const handleComplete = () => {
loading.value = false;
loadingError.value = false;
loadingProgress.value = 100;
emit('onLoad', false);
emit('onComplete');
};
/**
* 处理PDF滚动事件
* @param {number} scrollOffset - 滚动偏移量
*/
const handleScroll = (scrollOffset) => {
// 可以在这里处理滚动事件
};
/**
* 处理页面变化事件
* @param {number} page - 当前页码
*/
const handlePageChange = (page) => {
// 可以在这里处理页面变化事件
};
/**
* 处理PDF初始化完成
* @param {Object} pdf - PDF文档对象
*/
const handlePdfInit = (pdf) => {
// PDF初始化完成后的处理
loadingError.value = false;
nextTick(() => {
if (props.preventSave) {
// 添加防止保存的事件监听器
addPreventSaveListeners();
}
});
};
/**
* 处理PDF加载错误
* @param {Error} error - 错误对象
*/
const handlePdfError = (error) => {
console.error('PDF加载失败:', error);
loading.value = false;
loadingError.value = true;
loadingProgress.value = 0;
// 根据错误类型设置不同的错误信息
if (error.name === 'NetworkError' || error.message.includes('network')) {
errorMessage.value = '网络连接失败,请检查网络后重试';
} else if (error.name === 'InvalidPDFException' || error.message.includes('Invalid PDF')) {
errorMessage.value = 'PDF文件格式错误或已损坏';
} else if (error.message.includes('404') || error.message.includes('Not Found')) {
errorMessage.value = '文件不存在或已被删除';
} else if (error.message.includes('403') || error.message.includes('Forbidden')) {
errorMessage.value = '没有权限访问此文件';
} else {
errorMessage.value = '文件加载失败,请重试';
}
};
/**
* 重试加载PDF
*/
const retryLoad = () => {
loadingError.value = false;
loading.value = true;
loadingProgress.value = 0;
errorMessage.value = '';
};
/**
* 添加防止保存的事件监听器
*/
const addPreventSaveListeners = () => {
const pdfContainer = document.querySelector('.pdf-viewer-container .pdf-content');
if (pdfContainer) {
// 防止上下文菜单(右键菜单)
pdfContainer.addEventListener('contextmenu', (e) => {
e.preventDefault();
return false;
});
// 防止长按选择
pdfContainer.addEventListener('selectstart', (e) => {
e.preventDefault();
return false;
});
// 防止拖拽
pdfContainer.addEventListener('dragstart', (e) => {
e.preventDefault();
return false;
});
// 防止触摸回调(移动端长按)
pdfContainer.addEventListener('touchstart', (e) => {
if (e.touches.length > 1) {
e.preventDefault();
}
});
// 防止双指缩放等手势
pdfContainer.addEventListener('gesturestart', (e) => {
e.preventDefault();
});
}
};
</script>
<style scoped>
.pdf-viewer-container {
width: 100%;
height: 100%;
position: relative;
background-color: #f5f5f5;
}
.pdf-content {
width: 100%;
height: 100%;
overflow: auto;
}
/* 防止PDF内容被选择和保存的样式 */
.pdf-no-select {
-webkit-user-select: none;
/* Safari */
-moz-user-select: none;
/* Firefox */
-ms-user-select: none;
/* IE10+/Edge */
user-select: none;
/* Standard */
-webkit-touch-callout: none;
/* iOS Safari */
-webkit-tap-highlight-color: transparent;
/* 移除点击高亮 */
pointer-events: auto;
/* 保持可点击 */
}
/* 深度选择器,防止PDF内部元素被保存 */
:deep(.pdf-no-select img) {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: transparent;
pointer-events: none;
/* 禁用图片的所有交互 */
-webkit-user-drag: none;
/* 禁止拖拽 */
-khtml-user-drag: none;
-moz-user-drag: none;
-o-user-drag: none;
user-drag: none;
}
/* 防止Canvas元素被保存 */
:deep(.pdf-no-select canvas) {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: transparent;
pointer-events: none;
-webkit-user-drag: none;
-khtml-user-drag: none;
-moz-user-drag: none;
-o-user-drag: none;
user-drag: none;
}
/* 关闭按钮样式 */
.close-btn {
position: fixed;
right: 20px;
top: 20px;
z-index: 100;
width: 40px;
height: 40px;
padding: 0;
border-radius: 50%;
background: rgba(0, 0, 0, 0.6);
color: #fff;
}
/* 加载遮罩样式 */
.wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
background: rgba(0, 0, 0, 0.7);
backdrop-filter: blur(4px);
}
/* 加载内容样式 */
.loading-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
color: #fff;
}
.progress-circle {
width: 80px;
height: 80px;
background-color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 20px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.progress-percent {
font-size: 18px;
font-weight: bold;
color: #1989fa;
}
.loading-text {
max-width: 200px;
}
.loading-title {
font-size: 16px;
font-weight: 500;
margin-bottom: 8px;
color: #fff;
}
.loading-subtitle {
font-size: 14px;
color: rgba(255, 255, 255, 0.8);
line-height: 1.4;
}
/* 错误状态样式 */
.error-container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
background: #f8f9fa;
}
.error-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 40px 20px;
max-width: 300px;
}
.error-title {
font-size: 18px;
font-weight: 600;
color: #333;
margin: 16px 0 8px 0;
}
.error-message {
font-size: 14px;
color: #666;
line-height: 1.5;
margin-bottom: 20px;
}
.retry-btn {
min-width: 100px;
}
</style>