本文使用的是 vue-pdf,其实还有其他的很多比如pdf.js,只不过觉得这个和vue结合了应该不用下载一堆东西,直接npm install就可以,所以采用vue-pdf来撰写pdf下载及预览。
无论是预览还是下载,都需要安装vue-pdf
先看下效果:
![](https://img-blog.csdnimg.cn/20200402232157402.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NjUwMjA4,size_16,color_FFFFFF,t_70)
1、安装vue-pdf
npm install --save vue-pdf
2、在需要的组件里面引用
import pdf from 'vue-pdf'
components: {pdf},
3、在需要的vue文件中引入vue-pdf,pdf引入以及使用的位置,如下图:
![](https://img-blog.csdnimg.cn/20200402224754552.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NjUwMjA4,size_16,color_FFFFFF,t_70)
一、pdf下载
1、先在template中定义一个下载按钮,添加事件
下载
2、methods中定义方法:
gotoOption (val, item, index) {
if (val === 'collect') {
this.collectReport(item, index)
} else if (val === 'downLoad') {
this.downloadWeekly(item['local_access_full-text-link'], item['article_article-title'], item['uuid'])
}
},
// 下载
downloadWeekly (url, pdfName, uuid) {
// 调用子组件的下载方法
this.downloadPDF(url, pdfName, uuid)
},
downloadPDF (url, fileName, uuid) {
const _this = this
fetch(url).then(function (response) {
if (response.ok) {
return response.arrayBuffer()
}
throw new Error('Network response was not ok.')
}).then(function (arraybuffer) {
let blob = new Blob([arraybuffer], {
type: `application/pdf;charset-UTF-8` // word文档为msword,pdf文档为pdf
})
let objectURL = URL.createObjectURL(blob)
let downEle = document.createElement('a')
let fname = fileName // 下载文件的名字
// let fname = `download` // 下载文件的名字
downEle.href = objectURL
downEle.setAttribute('download', fname)
document.body.appendChild(downEle)
downEle.click()
DownloadArticleList({ // 此处是调用接口,将下载的文件信息传给后台
uuid: uuid,
openid: _this.openid
}).then(res => {
if (res.data.errno === 0) {
alert('下载完成')
}
console.log('xiazai:', res.data)
})
}).catch(function (error) {
console.log('There has been a problem with your fetch operation: ', error.message)
})
},
以上就是pdf下载
二、pdf预览
一共有两种方法,一种是分页,还可以放大缩小,另一种是不分页,还未实现放大缩小
1、第一种是分页的pdf预览,也可以实现放大,代码如下:
下载详情
上一页
{{currentPage}} / {{pageCount}}
下一页
import pdf from 'vue-pdf'
export default {
metaInfo: {
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width,initial-scale=1.0, maximum-scale=2.0, user-scalable=yes' }
]
},
components: {pdf},
data () {
return {
currentPage: 0, // pdf文件页码
pageCount: 0, // pdf文件总页数
fileType: 'pdf', // 文件类型
src: '', // pdf文件地址
}
},
computed: {
pdfInfo () { // 这个是路由跳转携带的关于pdf的信息,包含链接,链接是可以直接打开pdf文件的
return this.$route.query.pdfInfo
}
},
created () {
// 有时PDF文件地址会出现跨域的情况,这里最好处理一下
this.src = pdf.createLoadingTask(this.pdfInfo.url)
},
methods: {
returnBack () {
this.$router.push({name: 'myDownLoad'})
},
// 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
changePdfPage (val) {
// console.log(val)
if (val === 0 && this.currentPage > 1) {
this.currentPage--
// console.log(this.currentPage)
}
if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage++
// console.log(this.currentPage)
}
},
// pdf加载时
loadPdfHandler (e) {
this.currentPage = 1 // 加载的时候先加载第一页
}
}
}
.collect_info {
height: 100%;
display: flex;
flex-direction: column;
/*background: #f8f8f8;*/
position: relative;
}
.score_header {
text-align: center;
letter-spacing: 0.2em;
position: relative;
font-size: 18px;
width: 100%;
background-color: rgb(0, 115, 231);
color: #fff;
height: 45px;
line-height: 45px;
flex: none;
z-index: 1;
}
.return__icon{
position: absolute;
margin-left: 15px;
}
.iconfanhui {
margin-top: 10px;
font-size: 20px;
}
.pdf {
text-align: center;
.arrow {
margin: 8px 0;
/*font-size: 14px;*/
}
.grey {
color: #9c9c9c;
}
}
pdf链接打开是这样的:
![](https://img-blog.csdnimg.cn/20200402230545489.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NjUwMjA4,size_16,color_FFFFFF,t_70)
2、 第二种是不分页的不可以实现放大缩小,但是可以上下滚动
下载详情
import pdf from 'vue-pdf'
import BScroll from 'better-scroll'
export default {
metaInfo: {
meta: [
{ name: 'viewport', content: 'width=device-width,initial-scale=1.0, maximum-scale=2.0, user-scalable=yes' }
]
},
components: {pdf},
data () {
return {
fileType: 'pdf', // 文件类型
src: '', // pdf文件地址
numPages: 0, // 总页数
pdfScroll: null,
}
},
computed: {
pdfInfo () { // 路由携带的参数,包括pdf链接
return this.$route.query.pdfInfo
}
},
created () {
// 有时PDF文件地址会出现跨域的情况,这里最好处理一下
this.src = pdf.createLoadingTask(this.pdfInfo.url)
this.src.promise.then(pdf => {
this.numPages = pdf.numPages
})
this.init()
},
methods: {
returnBack () {
this.$router.push({name: 'myDownLoad'})
},
init () {
this.$nextTick(() => {
this.pdfScroll = new BScroll(this.$refs.pdfView, {
click: true
})
})
},
}
}
.collect_info {
height: 100%;
display: flex;
flex-direction: column;
/*background: #f8f8f8;*/
position: relative;
}
.score_header {
text-align: center;
letter-spacing: 0.2em;
position: relative;
font-size: 18px;
width: 100%;
background-color: rgb(0, 115, 231);
color: #fff;
height: 45px;
line-height: 45px;
flex: none;
z-index: 1;
}
.return__icon{
position: absolute;
margin-left: 15px;
}
.iconfanhui {
margin-top: 10px;
font-size: 20px;
}
.pdfIn {
height: calc(100% - 50px);
overflow: hidden;
}
以上就是关于pdf下载和预览的一些分享,哪里有不对的,欢迎指正~
|