<template> <div class="pdf-wrapper"> <!-- PDF展示部分 --> <div class="pdf-content"> <div class="pdf-contain"> <pdf v-for="item in numPages" :key="item" :src="src" :page="item" ref="pdf" /> </div> </div> <div style="text-align:center;"> <a-button type="primary" @click="FileDownload" icon="download">下载</a-button> </div> </div> </template> <script> import pdf from 'vue-pdf' import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js' import axios from 'axios' export default { name: 'pdfView', components: { pdf, }, data() { return { src: '', numPages: '', }; }, props: { value: { type: String, default() { return null }, }, fileName: { type: String, default() { return null } } }, mounted() { this.getTitlePdfurl() }, methods: { getTitlePdfurl() { this.src = pdf.createLoadingTask({ url: this.value, CMapReaderFactory, cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.5.207/cmaps/', cMapPacked: true }) //解决中文乱码问题 this.src.promise.then((pdf) => { this.numPages = pdf.numPages }, err => { console.log('pdf文档加载失败!error:' + err) }) }, FileDownload() { axios({ url: this.value, // 替换为实际文件的URL method: 'GET', responseType: 'blob' // 告诉axios返回的数据类型为Blob }).then(response => { const url = window.URL.createObjectURL(new Blob([response.data])) const link = document.createElement('a') link.href = url link.setAttribute('download', this.fileName) // 下载文件的名称 document.body.appendChild(link) link.click() }) } }, }; </script> <style lang="less" scoped> .pdf-wrapper { width: 100%; height: 600px; display: flex; justify-content: center; flex-direction: column; } .pdf-wrapper span { width: 100%; } .tools { width: 100%; height: 70px; display: flex; align-items: center; justify-content: space-around; z-index: 999; margin-bottom: -3%; padding-top: 2%; } .pdf-content { display: flex; justify-content: center; width: 100%; height: 98%; border-radius: 50px; overflow-x: hidden; .pdf-contain { width: 100%; overflow-x: hidden; .pdf-show { width: 100%; } } } </style>