Commit dac73c21 authored by wangqinghua's avatar wangqinghua

pdf预览

parent ee283bed
<template> <template>
<div class="pdf-view"> <div class="pdf-view">
<p class="title">PDF预览</p> <aside>
基于插件 <a href="https://github.com/FranckFreiburger/vue-pdf" target="_blank">vue-pdf</a>
</aside>
<div class="demo-block demo">
<div class="source">
<p>例子:<el-button type="primary" @click="dialogVisible = true">PDF预览</el-button>
</p>
</div>
<div class="meta">
<div class="description">
<p><code>1)html</code>代码</p>
</div>
<div class="highlight">
<pre v-highlight><code class="hljs">
{{html1}}
</code></pre>
</div>
<div class="description">
<p>2)<code>js</code>代码</p>
</div>
<div class="highlight">
<pre v-highlight><code class="hljs">
import pdf from 'vue-pdf'
components: { pdf },
//变量
src: '',
currentPage: 1,
pageCount: 0,
percentage: 0,
changePdfPage(val) {
if (val === 0 && this.currentPage > 1) {
this.currentPage--
}
if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage++
}
},
// pdf加载时
loadPdfHandler(e) {
this.currentPage = 1 // 加载的时候先加载第一页
},
// 加载中进度条
loadedProcess(e) {
this.percentage = Number((100 * e).toFixed(1))
}
</code></pre>
</div>
</div>
</div>
<!-- pdf预览弹窗 -->
<el-dialog :visible.sync = dialogVisible>
<p class="arrow"> <p class="arrow">
<!-- 上一页--> <!-- 上一页-->
<el-button size="small" type="primary" @click="changePdfPage(0)"> <el-button size="small" type="primary" @click="changePdfPage(0)">
...@@ -24,6 +80,7 @@ ...@@ -24,6 +80,7 @@
<div v-if="percentage < 100" class="process"> <div v-if="percentage < 100" class="process">
<el-progress type="circle" :percentage="percentage" /> <el-progress type="circle" :percentage="percentage" />
</div> </div>
</el-dialog>
</div> </div>
</template> </template>
...@@ -38,23 +95,46 @@ export default { ...@@ -38,23 +95,46 @@ export default {
src: 'http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf', src: 'http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf',
currentPage: 1, currentPage: 1,
pageCount: 0, pageCount: 0,
percentage: 0 percentage: 0,
dialogVisible: false,
html1:`
<p class="arrow">
<!--上一页-->
<el-button size="small" type="primary" @click="changePdfPage(0)">
下一页
</el-button>
{{ currentPage }} / {{ pageCount }}
<!-- 下一页-->
<el-button size="small" type="primary" @click="changePdfPage(1)">
下一页
</el-button>
</p>
<!--pdf预览-->
<pdf
ref="pdf"
:src="src"
:page="currentPage"
@progress="loadedProcess"
@num-pages="pageCount = $event"
@page-loaded="currentPage= $event"
@loaded="loadPdfHandler"
/>
<!--进度条-->
<div v-if="percentage < 100" class="process">
<el-progress type="circle" :percentage="percentage" />
</div>`
} }
}, },
created() { created() {
// this.src = pdf.createLoadingTask(this.src) // this.src = pdf.createLoadingTask(this.src)
}, },
methods: { methods: {
// 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
changePdfPage(val) { changePdfPage(val) {
// console.log(val)
if (val === 0 && this.currentPage > 1) { if (val === 0 && this.currentPage > 1) {
this.currentPage-- this.currentPage--
// console.log(this.currentPage)
} }
if (val === 1 && this.currentPage < this.pageCount) { if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage++ this.currentPage++
// console.log(this.currentPage)
} }
}, },
...@@ -65,14 +145,17 @@ export default { ...@@ -65,14 +145,17 @@ export default {
// 加载中进度条 // 加载中进度条
loadedProcess(e) { loadedProcess(e) {
console.log(e)
this.percentage = Number((100 * e).toFixed(1)) this.percentage = Number((100 * e).toFixed(1))
} }
} }
} }
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.pdf-view{
margin: 40px;
}
.title { .title {
text-align: center; text-align: center;
font-weight: bold; font-weight: bold;
...@@ -85,7 +168,4 @@ export default { ...@@ -85,7 +168,4 @@ export default {
margin: 60px; margin: 60px;
} }
pdf > span {
width: 100%;
}
</style> </style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment