PdfPreview.vue
2.81 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
<!--
* @Date: 2024-01-17
* @Description: PDF预览组件
-->
<template>
<van-popup v-if="show" :show="show" @update:show="emit('update:show', $event)" position="right"
:style="{ height: '100%', width: '100%' }">
<div id="pdf-container"></div>
<van-button class="close-btn" type="default" icon="cross" round @click="emit('update:show', false)" />
</van-popup>
</template>
<script setup>
import { ref, nextTick, onMounted, computed } from 'vue';
import { initPdfView, configPdfApiOptions } from "@sunsetglow/vue-pdf-viewer";
import "@sunsetglow/vue-pdf-viewer/dist/style.css";
const props = defineProps({
show: {
type: Boolean,
default: false
},
url: {
type: String,
default: ''
},
title: {
type: String,
default: ''
}
});
const emit = defineEmits(['update:show']);
// PDF Worker路径
const loading = ref(false);
const pdfPath = new URL("@sunsetglow/vue-pdf-viewer/dist/libs/pdf.worker.min.js", import.meta.url).href;
// 监听show属性变化
watch(() => props.show, (newVal) => {
if (newVal && props.url) {
initPdfViewer();
}
});
// 初始化PDF预览
// 查看文档 https://www.npmjs.com/package/@sunsetglow/vue-pdf-viewer
const initPdfViewer = () => {
nextTick(() => {
initPdfView(document.querySelector("#pdf-container"), {
loadFileUrl: props.url,
pdfPath: pdfPath,
loading: (load, fileInfo) => {
loading.value = load;
},
pdfOption: {
search: false,
scale: true,
pdfImageView: true,
page: true,
navShow: true,
navigationShow: false,
pdfViewResize: true,
toolShow: true,
download: true,
clearScale: 1.5,
fileName: props.title,
lang: "en",
print: false,
watermarkOptions: undefined,
customPdfOption: {
// customPdfOption是 pdfjs getDocument 函数中一些配置参数 具体可参考 https://mozilla.github.io/pdf.js/api/draft/module-pdfjsLib.html#~DocumentInitParameters
cMapPacked: true, //指定 CMap 是否是二进制打包的
cMapUrl: "https://cdn.jsdelivr.net/npm/pdfjs-dist@2.2.228/cmaps/", //预定义 Adobe CMaps 所在的 URL。可解决字体加载错误
},
}
});
});
};
</script>
<style scoped>
#pdf-container {
width: 100%;
padding: 0px;
height: 100%;
}
.close-btn {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 100;
width: 40px;
height: 40px;
padding: 0;
border-radius: 50%;
background: rgba(0, 0, 0, 0.6);
color: #fff;
}
</style>