emis-frontend/src/components/PdfViewer/viewer.vue

361 lines
9.1 KiB
Vue
Raw Normal View History

2024-05-31 10:15:03 +00:00
<template>
<!-- v-if="pdfUrl" -->
<div class="pdf-viewer grid2" >
<div style="position: relative;">
<div class="pdf-bar">
<div class="zoom-minus" @click="zoomIn">
<i class="el-icon-minus" title="缩小"></i>
</div>
<div class="scale" >
<el-input v-model.number="scale" size="mini" @keyup.enter.native="setScaleValue">
<template slot="append">%</template>
</el-input>
</div>
<div class="zoom-plus" @click="zoomOut">
<i class="el-icon-plus" title="放大"></i>
</div>
<div class="prev" @click="preFile">
上一页
</div>
<div class="page">
<el-input-number
v-model.number="currentNum"
:min="0"
:max="fileCount"
size="mini"
/>
</div>
<div class="page-count">/ {{ fileCount }}</div>
<div class="next" @click="nextFile">
下一页
</div>
<div class="tools">
<div style="width:100%;text-align: right;">
<el-button title="下载" type="primary" icon="el-icon-download" @click="downPDF">下载</el-button>
<el-button title="打印" type="primary" icon="el-icon-printer" @click="printPDF">打印</el-button>
</div>
</div>
</div>
<div style="min-height:1600px;margin: 0 auto;overflow: auto;">
<div class="pdf-wrapper">
<!-- v-for="item in pdfList"
:key="item.billCode"
:src="item.pdfUrl" -->
<pdf
:src="currentPdfUrl"
:page="page"
@num-pages="pageCount = $event"
@page-loaded="onPageLoaded($event)"
ref="pdf"
></pdf>
<!-- v-drag -->
</div>
</div>
</div>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: { pdf },
props: {
pdfList:{
type: Array,
},
startPage: {
type: Number,
default: 1
}
},
data() {
return {
cacheFileList:this.pdfList,
loadPdfUrl:null,
page: 1,
currentPage: 0,
pageCount: 0,
currentNum:1,
fileCount: this.pdfList.length,
scale: 20,
preScale: 20,
}
},
computed: {
currentPdfUrl() {
if (!this.loadPdfUrl) return ''
const publicPath =
process.env.NODE_ENV === 'production'
? process.env.VUE_APP_ROUTER_BASE
: '/'
return pdf.createLoadingTask({
url: this.loadPdfUrl,
cMapUrl: `${publicPath}cmaps/`,
cMapPacked: true
})
}
},
watch: {
pdfList(){
console.log("===77777====>");
this.cacheFileList=this.pdfList;
// this.loadPdfUrl =this.cacheFileList[0].pdfUrl;
this.fileCount=this.cacheFileList.length;
// console.log(this.loadPdfUrl);
},
pageCount(value) {
if (value) this.page = this.startPage
}
},
mounted() {
if(this.cacheFileList&& this.cacheFileList.length>0){
this.loadPdfUrl =this.pdfList[0].pdfUrl;
}
window.addEventListener('mousewheel', this.handleScroll, { passive: false })
},
methods: {
addPdfUrl(ydSn,pdfUrl){
let item={
"ydSn":ydSn,
"pdfUrl":pdfUrl
}
this.pdfList.push(item);
},
handleScroll(e){
// 判断是不是按下ctrl键
if(e.ctrlKey) {
// 取消浏览器默认的放大缩小网页行为
e.preventDefault()
// 判断是向上滚动还是向下滚动
if (e.deltaY > 0) {
// 放大重写,业务代码
this.zoomOut()
} else {
// 缩小重写,业务代码
this.zoomIn()
}
}
},
onPageLoaded(){
this.doScale(20);
},
doScale(val){
if (isNaN(val)) {
this.scale = this.preScale
return
}
if (val< 10) {
this.scale = 10
}
if (val > 200) {
this.scale = 200
}
this.preScale = this.scale
// this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%';
// this.$refs.pdf.$el.style.margin = "0 auto";
this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%';
this.$refs.pdf.$el.style.margin = "0 auto";
},
setScaleValue(e) {
this.doScale(e.target.value)
},
zoomIn() {
if (this.scale > 20) {
this.scale += -5
this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
this.$refs.pdf.$el.style.margin = "0 auto";
}
},
zoomOut() {
if (this.scale < 200) {
this.scale += 5
this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
this.$refs.pdf.$el.style.margin = "0 auto";
}
},
preFile(){
let count = this.currentNum - 1
if (count < 1) return
this.currentNum = count
this.loadPdfUrl =this.cacheFileList[this.currentNum-1].pdfUrl;
},
nextFile() {
console.log('this.fileCount', this.currentNum, this.fileCount)
let count = this.currentNum + 1
if (count > this.fileCount) return
this.currentNum = count
this.loadPdfUrl =this.cacheFileList[this.currentNum-1].pdfUrl;
},
prePage() {
let count = this.currentPage - 1
if (count < 1) return
this.page = count
},
nextPage() {
console.log('this.pageCount', this.currentPage, this.pageCount)
let count = this.currentPage + 1
if (count > this.pageCount) return
this.page = count
},
// 下载
downPDF() {
var tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = this.loadPdfUrl
tempLink.setAttribute('download', (new Date()).getTime()+'.pdf')
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank')
}
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
},
// 打印
printPDF() {
// this.$refs.pdf.print()
this.$emit("onPrint");
}
}
}
</script>
<style lang="scss" scoped>
.grid1 {
height: 200px;
background-image:
linear-gradient(45deg, #8d8b8b 25%, transparent 0),
linear-gradient(-45deg, #8d8b8b 25%, transparent 0),
linear-gradient(45deg, transparent 75%, #8d8b8b 0),
linear-gradient(-45deg, transparent 75%, #8d8b8b 0);
background-position: 0 0, 0 10px, 10px -10px, -10px 0;
background-size: 20px 20px;
}
.grid2 {
height: 200px;
background-image: radial-gradient(circle, rgb(165, 165, 165) 1px, #fff 1px);
background-size: 20px 20px;
background-position: center center;
}
.grid3 {
height: 200px;
background-image:
linear-gradient(to right, rgb(203 213 225) 1px, transparent 1px),
linear-gradient(to bottom, rgb(203 213 225) 1px, transparent 1px);
background-size: 20px 20px;
background-position: center center;
}
.grid4 {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Crect width='40' height='40' fill='%23fff' /%3E%3Crect x='50%' width='1' height='100%' fill='rgb(203 213 225)' /%3E%3Crect y='50%' width='100%' height='1' fill='rgb(203 213 225)' /%3E%3C/svg%3E%0A");
}
.pdf-viewer {
position: relative;
width: 100%;
height: 100%;
// overflow: hidden;
overflow: auto;
// height: 100%;
font-family: PingFang SC;
// width: 100%;
// position: relative;
// background: #eeeeee;
// background: #dcdcdc;
.tools {
display: flex;
z-index: 9999;
position: absolute;
top: 5px;
right: 30px;
}
.pdf-bar {
position: absolute;
// position: fixed;
top: 0px;
left: 0px;
z-index: 1;
width: 100%;
height: 50px;
border: 1px solid #ddd;
box-sizing: border-box;
text-align: center;
background: #f8f8f8;
display: flex;
justify-content: center;
align-items: center;
& > div {
min-width: 36px;
height: 36px;
line-height: 36px;
color: #666;
cursor: pointer;
i {
width: 100%;
height: 100%;
color: #666;
font-weight: bold;
}
::v-deep .el-input--mini {
input {
padding: 0 8px;
width: 50px;
text-align: center;
}
.el-input-group__append {
padding: 0 6px;
color: #666;
cursor: auto;
}
}
::v-deep .el-input-number {
margin-left: 10px;
width: 40px;
.el-input-number__decrease {
display: none;
}
.el-input-number__increase {
display: none;
}
.el-input--mini {
input {
width: 40px;
text-align: center;
}
}
}
}
.prev {
margin-left: 16px;
}
.scale,
.page {
display: flex;
align-items: center;
}
.page-count {
margin-right: 10px;
}
}
.pdf-wrapper {
position: absolute;
margin-top: 70px;
width: 100%;
height: calc(100% - 70px);
overflow: auto;
}
}
</style>