vue中使用vue-pdf的方法詳解
阿新 • • 發佈:2020-09-07
需求:簡單說~~有兩個pdf檔案需在h5上展示,通過點選按鈕切換不同檔案的顯示
注:
1.vue-pdf預設展示首頁,我這裡的需求是通過滑動展示所有頁面,這裡使用的v-for遍歷。有多少頁就載入了多少個pdf元件。
2.pdf檔案存在跨域問題,這個需要後端同學支援。
3.demo上的pdf檔案只有一頁,測試多頁展示,自己改用多頁pdf檔案即可
<template> <div class="pdf_wrap"> <div class="pdf_list"> <!-- src:pdf地址,page: 當前顯示頁 --> <pdf v-for="i in numPages" :key="i" :src="src" :page="i" style="width: 100%" > </pdf> </div> <Button type="info" @click="loadPdf(pdfUrl1)"> 檔案1 </Button> <Button type="info" native-type="submit" @click="loadPdf(pdfUrl2)"> 檔案2 </Button> </div> </template> <script> import pdf from 'vue-pdf' import { Button } from 'vant' export default { components: { pdf,Button },data () { return { src: '',numPages: undefined,pdfUrl1: 'https://clinic-trial-attachments.oss-cn-beijing.aliyuncs.com/output/demo.pdf/1.pdf',pdfUrl2: 'https://clinic-trial-attachments.oss-cn-beijing.aliyuncs.com/output/123demo' } },mounted () { this.loadPdf(this.pdfUrl1) },methods: { loadPdf (url) { this.src = pdf.createLoadingTask(url) this.src.promise.then(pdf => { this.numPages = pdf.numPages // 這裡拿到當前pdf總頁數 }) } } } </script> <style scoped> .pdf_wrap { background: #fff; height: 100vh } .pdf_list { height: 80vh; overflow: scroll; } button { margin-bottom: 20px; } </style>
總結
到此這篇關於vue中使用vue-pdf的方法詳解的文章就介紹到這了,更多相關vue使用vue-pdf內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!