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
<template>
<div class="pdf-wrapper">
<!-- PDF展示部分 -->
<div class="pdf-content">
<div class="pdf-contain">
<pdf v-for="item in numPages" :key="item" :src="src" :page="item" ref="pdf" />
</div>
</div>
<div style="text-align:center;">
<a-button type="primary" @click="FileDownload" icon="download">下载</a-button>
</div>
</div>
</template>
<script>
import pdf from 'vue-pdf'
import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'
import axios from 'axios'
export default {
name: 'pdfView',
components: {
pdf,
},
data() {
return {
src: '',
numPages: '',
};
},
props: {
value: {
type: String,
default() {
return null
},
},
fileName: {
type: String,
default() {
return null
}
}
},
mounted() {
this.getTitlePdfurl()
},
methods: {
getTitlePdfurl() {
this.src = pdf.createLoadingTask({
url: this.value,
CMapReaderFactory,
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.5.207/cmaps/',
cMapPacked: true
}) //解决中文乱码问题
this.src.promise.then((pdf) => {
this.numPages = pdf.numPages
}, err => {
console.log('pdf文档加载失败!error:' + err)
})
},
FileDownload() {
axios({
url: this.value, // 替换为实际文件的URL
method: 'GET',
responseType: 'blob' // 告诉axios返回的数据类型为Blob
}).then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', this.fileName) // 下载文件的名称
document.body.appendChild(link)
link.click()
})
}
},
};
</script>
<style lang="less" scoped>
.pdf-wrapper {
width: 100%;
height: 600px;
display: flex;
justify-content: center;
flex-direction: column;
}
.pdf-wrapper span {
width: 100%;
}
.tools {
width: 100%;
height: 70px;
display: flex;
align-items: center;
justify-content: space-around;
z-index: 999;
margin-bottom: -3%;
padding-top: 2%;
}
.pdf-content {
display: flex;
justify-content: center;
width: 100%;
height: 98%;
border-radius: 50px;
overflow-x: hidden;
.pdf-contain {
width: 100%;
overflow-x: hidden;
.pdf-show {
width: 100%;
}
}
}
</style>