pdfPreview.vue
3.02 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
<template>
<div class="pdf-preview">
<div class="pdf-wrap">
<vue-pdf-embed :source="state.source" :style="scale" :scale="state.scale" class="vue-pdf-embed" :page="state.pageNum" />
</div>
<div class="page-tool">
<div class="page-tool-item" @click="lastPage">上一页</div>
<div class="page-tool-item" @click="nextPage">下一页</div>
<div class="page-tool-item">{{state.pageNum}}/{{state.numPages}}</div>
<div class="page-tool-item" @click="pageZoomOut">放大</div>
<div class="page-tool-item" @click="pageZoomIn">缩小</div>
</div>
</div>
</template>
<script setup lang="ts">
import VuePdfEmbed from "vue-pdf-embed";
// import { createLoadingTask } from "vue3-pdfjs/esm"; // 获得总页数
// import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'
import * as pdfjsLib from "pdfjs-dist";
/*import * as pdfjsWorker from 'pdfjs-dist/build/pdf.worker.mjs'
(window as any).pdfjsWorker = pdfjsWorker*/
const props = defineProps({
pdfUrl: {
type: String,
required: true
}
})
const state = reactive({
source: props.pdfUrl, // 预览pdf文件地址
// source: '/src/assets/images/home/1.pdf', // 预览pdf文件地址
/*source: {
url: props.pdfUrl,
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.9.359/cmaps/',
cMapPacked: true,
},*/
pageNum: 1, // 当前页面
scale: 1, // 缩放比例
numPages: 0, // 总页数
});
const scale = computed(() => `transform:scale(${state.scale});transform-origin: 50% 0`)
function lastPage() {
if (state.pageNum > 1) {
state.pageNum -= 1;
}
}
function nextPage() {
if (state.pageNum < state.numPages) {
state.pageNum += 1;
}
}
function pageZoomOut() {
if (state.scale < 2) {
state.scale += 0.1;
}
}
function pageZoomIn() {
if (state.scale > 0.5) {
state.scale -= 0.1;
}
}
onMounted(()=> {
pdfjsLib.GlobalWorkerOptions.workerSrc = "/pdf.worker.js";
const loadingTask = pdfjsLib.getDocument(state.source);
// const loadingTask = createLoadingTask({url: state.source, CMapReaderFactory});
loadingTask.promise.then((pdf:{numPages: number}) => {
state.numPages = pdf.numPages;
});
})
</script>
<style lang="css" scoped>
.pdf-preview {
position: relative;
height: 80vh;
padding: 20px 0;
box-sizing: border-box;
background-color: #e9e9e9;
}
.pdf-wrap{
overflow-y:auto ;
}
.vue-pdf-embed {
text-align: center;
height: 68vh;
width: 50%;
border: 1px solid #e5e5e5;
margin: 0 auto;
box-sizing: border-box;
}
.page-tool {
position: absolute;
bottom: 35px;
padding-left: 15px;
padding-right: 15px;
display: flex;
align-items: center;
background: rgb(66, 66, 66);
color: white;
border-radius: 19px;
z-index: 100;
cursor: pointer;
margin-left: 50%;
transform: translateX(-50%);
}
.page-tool-item {
padding: 8px 15px 8px 10px;
cursor: pointer;
user-select: none;
}
</style>