Pdf.vue
3.4 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
<template>
<el-dialog
:visible.sync="pdfDialog"
:close-on-click-modal="false"
:close-on-press-escape="false"
width="80%"
:show-close="false"
top="52px"
>
<p class="arrow">
<el-button
icon="el-icon-download"
@click="download()"
style="download"
type="primary"
size="mini"
>下载</el-button
>
<span
style="
float: right;
padding-right: 40px;
font-size: 20px;
color: rgb(223 227 234);
cursor: pointer;
"
@click="handleClose"
>
<i style="font-size: 20px" class="el-icon-close"></i>
</span>
</p>
<iframe
:src="pdfSrc"
style="width: 100%; border: 0"
align="middle"
:height="height"
></iframe>
</el-dialog>
</template>
<script>
import {Message} from 'element-ui'
import req from '@/request.js'
export default {
name: 'pdf',
props: ['src', 'fileRow'],
data() {
return {
filesProps: {
label: 'originName',
},
pdfDialog: false,
currentPage: 0, // pdf文件页码
pageCount: 0, // pdf文件总页数
height: 800,
fileType: 'pdf', // 文件类型
pdfSrc: '',
}
},
mounted() {
this.height = document.documentElement.clientHeight - 80
},
watch: {
src: function (newVal, oldVal) {
if (newVal) {
let _this = this
req.get(this.src, 'arraybuffer').then((response) => {
_this.pdfSrc = window.URL.createObjectURL(
new Blob([response.data], {type: 'application/pdf'})
)
})
}
},
},
methods: {
// 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页 $scope.src=+portal+$scope.data.pdfUrl;
changePdfPage(val) {
if (val === 0 && this.currentPage > 1) {
this.currentPage--
}
if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage++
}
},
// pdf加载时
loadPdfHandler() {
this.currentPage = 1 // 加载的时候先加载第一页
},
handleOpen() {
this.pdfDialog = true
},
//关闭弹框,并且把附件列表的src变为默认"",否则watch无法一直监听第一个打开的预览文件,因为src不变
handleClose() {
this.pdfDialog = false
this.$emit('changeSrc')
},
//附件下载
download() {
req.download(
'${portal}/system/file/v1/downloadFile?fileId=' + this.fileRow.id
)
},
},
}
</script>
<style lang="scss" scoped>
.currentPage {
cursor: pointer;
color: #8c8e92;
}
.currentPage:hover {
color: #2761ff;
}
.arrow {
position: fixed;
top: 0px;
z-index: 2;
width: 100%;
background-color: #191919;
padding: 12px 0;
margin: 0;
text-align: center;
}
/deep/.el-dialog__body {
height: calc(100% - 52px);
color: #606266;
font-size: 14px;
padding: 0;
padding-left: 30px;
}
/deep/.el-dialog {
height: calc(100% - 52px);
position: relative;
margin: 0 auto 0;
background: #fff;
border-radius: 2px;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 50%;
}
</style>