我是靠谱客的博主 欢喜胡萝卜,这篇文章主要介绍vue element-ui读取pdf文件的方法,现在分享给大家,希望可以做个参考。

本文实例为大家分享了vue element-ui 读取pdf文件,供大家参考,具体内容如下

添加依赖

复制代码
1
npm install pdfjs-dist --save

pdf.vue

复制代码
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
<template> <div class="app-container"> <el-dialog v-loading="loading" :visible.sync="dialogSeeVisible" :title="dialogTitle" :close-on-click-modal="closeModel" modal width="80%" @close="closeDialog" @open="onOpen" > <el-card class="cpdf"> <div class="center"> <div class="contor"> <el-button @click="prev">上一页</el-button> <el-button @click="next">下一页</el-button> <span>Page: <span v-text="page_num"/> / <span v-text="page_count"/></span> <el-button icon="el-icon-plus" @click="addscale"/> <el-button icon="el-icon-minus" @click="minus"/> <el-button id="prev" @click="closeDialog">关闭</el-button> </div> <canvas id="the-canvas" class="canvasstyle"/> </div> </el-card> </el-dialog> </div> </template> <script> import PDFJS from 'pdfjs-dist' PDFJS.GlobalWorkerOptions.workerSrc = './../../../node_modules/pdfjs-dist/build/pdf.worker.js' import request from '@/utils/request' import { Message } from 'element-ui' export default { name: 'pdf', props: { dialogSeeVisible: { type: Boolean, default: false }, seeFileId: { type: Number, default: null } }, data() { return { closeModel: false, clearable: false, urlPrefix: process.env.BASE_API, dialogTitle: '浏览技术文档', pdfurl: '', loading: false, pdfDoc: null, // pdfjs 生成的对象 pageNum: 1, // pageRendering: false, pageNumPending: null, scale: 1.2, // 放大倍数 page_num: 0, // 当前页数 page_count: 0, // 总页数 maxscale: 2, // 最大放大倍数 minscale: 0.8// 最小放大倍数 } }, computed: { ctx() { const id = document.getElementById('the-canvas') return id.getContext('2d') } }, created() { this.onOpen() }, methods: { closeDialog(freshList) { const _this = this _this.pdfurl = '' _this.pdfDoc = null _this.pageNum = 1 _this.pageRendering = false _this.pageNumPending = null _this.scale = 1.2 _this.page_num = 0 _this.page_count = 0 // PDFJS.getDocument(_this.pdfurl).then(function(pdfDoc_) { // _this.pdfDoc = pdfDoc_ // _this.page_count = _this.pdfDoc.numPages // _this.renderPage(_this.pageNum) // }) this.$emit('refreshValue', freshList) }, onOpen() { const _this = this _this.loading = true request({ url: '/document/info/preview/' + _this.seeFileId, method: 'get' }).then( function(value) { if (value.code === 200) { _this.pdfurl = _this.urlPrefix + '/' + value.data.fileVirtualPath _this.loading = false // 初始化pdf PDFJS.getDocument(_this.pdfurl).then(function(pdfDoc_) { _this.pdfDoc = pdfDoc_ _this.page_count = _this.pdfDoc.numPages _this.renderPage(_this.pageNum) }) } else { Message.error(value.message) _this.loading = false _this.closeDialog() } } ) }, renderPage(num) { // 渲染pdf const vm = this this.pageRendering = true const canvas = document.getElementById('the-canvas') // Using promise to fetch the page this.pdfDoc.getPage(num).then(function(page) { var viewport = page.getViewport(vm.scale) // alert(vm.canvas.height) canvas.height = viewport.height canvas.width = viewport.width // Render PDF page into canvas context var renderContext = { canvasContext: vm.ctx, viewport: viewport } var renderTask = page.render(renderContext) // Wait for rendering to finish renderTask.promise.then(function() { vm.pageRendering = false if (vm.pageNumPending !== null) { // New page rendering is pending vm.renderPage(vm.pageNumPending) vm.pageNumPending = null } }) }) vm.page_num = vm.pageNum }, addscale() { // 放大 if (this.scale >= this.maxscale) { return } this.scale += 0.1 this.queueRenderPage(this.pageNum) }, minus() { // 缩小 if (this.scale <= this.minscale) { return } this.scale -= 0.1 this.queueRenderPage(this.pageNum) }, prev() { // 上一页 const vm = this if (vm.pageNum <= 1) { return } vm.pageNum-- vm.queueRenderPage(vm.pageNum) }, next() { // 下一页 const vm = this if (vm.pageNum >= vm.page_count) { return } vm.pageNum++ vm.queueRenderPage(vm.pageNum) }, queueRenderPage(num) { if (this.pageRendering) { this.pageNumPending = num } else { this.renderPage(num) } } } } </script> <style scoped="" type="text/css"> .cpdf { top: 0; left: 0; background-color: rgba(0, 0, 0, .5); width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; } .center { text-align: center; height: 100%; overflow: auto; padding-top: 20px; } .contor { margin-bottom: 10px; } .button-group { float: right; margin-top: 10px; margin-bottom: 10px; } </style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是欢喜胡萝卜最近收集整理的关于vue element-ui读取pdf文件的方法的全部内容,更多相关vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(295)

评论列表共有 0 条评论

立即
投稿
返回
顶部