
yarn add vue-pdf
html:
<template>
<div>
<pdf ref="pdf" v-for="i in numPages" :key="i" :page="i" :src="laoclUrl">>pdf>
div>
template>
<script>
import pdf from "vue-pdf";
export default {
name: "Home",
components: { pdf },
data() {
return {
numPages: 1, // pdf文件页数
laoclUrl: "" // 预览路径
};
},
mounted() {
this.previewPdf();
},
methods: {
// 简历预览
previewPdf() {
// pdf地址
let fileUrl = "http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf";
// 计算总页数
let loadingTask = pdf.createLoadingTask(fileUrl);
loadingTask.promise
.then(pdf => {
this.numPages = pdf.numPages;
})
.catch(err => {
console.error("pdf 加载失败", err);
});
this.laoclUrl = fileUrl;
}
}
};
script>

yarn add pdfjs
<template>
<div>
<iframe height="100%" width="100%" :src="`${getFilePath}`">iframe>
div>
template>
<script>
export default {
name: "PDFJSViewer",
props: {
fileName: String,
path: String
},
computed: {
getFilePath: function() {
if (this.fileName !== "") {
return this.path + "?file=" + this.fileName;
}
return this.path;
}
}
};
script>
<template>
<div class="index_container full">
<PDFJSViewer :path="`${laoclUrl}`" :fileName="'pdf preview'" />
div>
template>
<script>
import PDFJSViewer from "/src/components/pdfViewer/pdfViewer";
export default {
name: "Home",
components: { PDFJSViewer },
data() {
return {
// 预览路径
laoclUrl: ""
};
},
mounted() {
this.previewPdf();
},
methods: {
// 简历预览
previewPdf() {
this.laoclUrl = "http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf";
}
}
};
script>