一、引入pdfjs-dist、axios包
npm i pdfjs-dist axios --save
二、pdfViewer组件
<template>
<div
class="pdf-viewer"
tabIndex="1"
>
<!-- <div class="position page">
<span
class="span_1"
@click="pageUp"
>上一页</span>
<span>页码:{
{
`${
pageNo}/${
totals.length}` }}</span>
<span
class="span_2"
@click="pageDown"
>下一页</span>
</div> -->
<div
v-show="!copy"
class="position info"
>
<!-- <div style="font-size:12px;">
注意:此页面无法复制文本,由于部分条款较大,会导致卡顿,如需复制请到“条款标注”、“条款文本”复制,或者点击<a
:href="`/tool/viewer?url=${encodeURIComponent(pdfUrl)}`"
target="_blank"
>新窗口预览</a>
</div> -->
</div>
<div
id="mainContent"
ref="mainContent"
class="pdf-main-content"
>
<div
v-for="(item, index) in totals"
:id="`page${id}-${item}`"
:key="'view-'+index"
class="pdf-box"
>
<div style="padding: 10px;background: #fff;">{
{
item}}/{
{
totalLength}}</div>
<canvas
:id="`canvas-pdf${id}-${item}`"
class="canvas-pdf"
/>
</div>
</div>
</div>
</template>
<script>
import PDFJS from 'pdfjs-dist'
import axios from 'axios'
import 'pdfjs-dist/web/pdf_viewer.css'
const pdfjsWorker = import('pdfjs-dist/build/pdf.worker.entry');
pdfjsWorker.then(worker => {
PDFJS.GlobalWorkerOptions.workerSrc = worker;
})
export default {
name: 'PdfViewer',
props: {
pdfUrl: {
type: String,
default: ''
},
pdfInfo: {
type: Object,
default() {
return {
};
}
},
id: {
type: String,
default: ''
},
copy: {
type: Boolean,
default: false
},
totalLength: {
type: Number,
default(){
</