Detailed Explanation of Using vue pdf in vue

  • 2021-08-10 06:46:23
  • OfStack

Requirements: Simply put ~ ~ There are two pdf files that need to be displayed on h5. Click the button to switch the display of different files

Note:

1. vue-pdf shows the home page by default. My requirement here is to show all pages by sliding, using v-for traversal here. There are as many pdf components loaded as there are pages.

2. There is a cross-domain problem in 2. pdf file, which needs the support of backend classmates.

3. The pdf file on demo only has 1 page. Test multi-page display, and use multi-page pdf file instead


<template>
 <div class="pdf_wrap">
  <div class="pdf_list">
   <!-- src:pdf Address, page:  Current display page  -->
   <pdf v-for="i in numPages" :key="i" :src="src" :page="i" style="width: 100%" > </pdf>
  </div>
  <Button type="info" @click="loadPdf(pdfUrl1)">
    Documents 1
  </Button>
   <Button type="info" native-type="submit" @click="loadPdf(pdfUrl2)">
    Documents 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 //  Get the current here pdf Total pages 
   })
  }
 }
}
</script>
<style scoped>
 .pdf_wrap {
  background: #fff;
  height: 100vh
 }
 .pdf_list {
  height: 80vh;
  overflow: scroll;
 }
 button {
  margin-bottom: 20px;
 }
</style>

Summarize


Related articles: