首先要引入vue-pdf
我使用的是yarn add vue-pdf
首先要将流文件转为pdf
<template>
<v-pdf
v-for="i in numPages"
:key="i"
class="v-pdf-item"
:src="src"
:page="i"
/>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
'v-pdf': pdf
},
methods: {
// 流文件转pdf url
getObjectURL(file) {
let url = null
if (window.createObjectURL !== undefined) { // basic
url = window.createObjectURL(file)
} else if (window.webkitURL !== undefined) { // webkit or chrome
try {
url = window.webkitURL.createObjectURL(file)
} catch (error) {
//
}
} else if (window.URL !== undefined) { // mozilla(firefox)
try {
url = window.URL.createObjectURL(file)
} catch (error) {
//
}
}
return url
},
async reportPreview() {
const result = await 后台接口
const src = this.getObjectURL(result.data)
this.src = pdf.createLoadingTask(src)
this.src.promise.then(pdf => {
this.numPages = pdf.numPages
})
}
}
}
</script>
如果遇到报错就引入下边两个依赖基本上就能解决问题。
yarn add pdfjs-dist@2.5.207
yarn add vue-pdf@4.2.0