vue element-ui讀取pdf文件的方法
本文實(shí)例為大家分享了vue element-ui 讀取pdf文件,供大家參考,具體內(nèi)容如下
添加依賴
npm install pdfjs-dist --save
pdf.vue
<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">關(guān)閉</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: '瀏覽技術(shù)文檔',
pdfurl: '',
loading: false,
pdfDoc: null, // pdfjs 生成的對象
pageNum: 1, //
pageRendering: false,
pageNumPending: null,
scale: 1.2, // 放大倍數(shù)
page_num: 0, // 當(dāng)前頁數(shù)
page_count: 0, // 總頁數(shù)
maxscale: 2, // 最大放大倍數(shù)
minscale: 0.8// 最小放大倍數(shù)
}
},
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue結(jié)合el-table實(shí)現(xiàn)表格行拖拽排序(基于sortablejs)
這篇文章主要介紹了vue結(jié)合el-table實(shí)現(xiàn)表格行拖拽排序(基于sortablejs),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01
element-ui自定義message-box自定義樣式不生效的解決
這篇文章主要介紹了element-ui自定義message-box自定義樣式不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue中nprogress頁面加載進(jìn)度條的方法實(shí)現(xiàn)
這篇文章主要介紹了Vue中nprogress頁面加載進(jìn)度條的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談vue項(xiàng)目4rs vue-router上線后history模式遇到的坑
今天小編就為大家分享一篇淺談vue項(xiàng)目4rs vue-router上線后history模式遇到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue+Minio實(shí)現(xiàn)多文件進(jìn)度上傳的詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于如何利用vue+Minio實(shí)現(xiàn)多文件進(jìn)度上傳的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03
在Vue項(xiàng)目中用fullcalendar制作日程表的示例代碼
這篇文章主要介紹了在Vue項(xiàng)目中用fullcalendar制作日程表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

