2017-04-12 02:44:22 +00:00
|
|
|
<script>
|
2019-05-06 23:37:13 +00:00
|
|
|
import pdfjsLib from 'pdfjs-dist/build/pdf';
|
|
|
|
import workerSrc from 'pdfjs-dist/build/pdf.worker.min';
|
2017-04-12 02:44:22 +00:00
|
|
|
|
2018-10-10 07:13:34 +00:00
|
|
|
import page from './page/index.vue';
|
2017-04-12 02:44:22 +00:00
|
|
|
|
2018-10-10 07:13:34 +00:00
|
|
|
export default {
|
|
|
|
components: { page },
|
|
|
|
props: {
|
|
|
|
pdf: {
|
|
|
|
type: [String, Uint8Array],
|
|
|
|
required: true,
|
2017-04-12 02:44:22 +00:00
|
|
|
},
|
2018-10-10 07:13:34 +00:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
pages: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
document() {
|
|
|
|
return typeof this.pdf === 'string' ? this.pdf : { data: this.pdf };
|
2017-04-12 02:44:22 +00:00
|
|
|
},
|
2018-10-10 07:13:34 +00:00
|
|
|
hasPDF() {
|
|
|
|
return this.pdf && this.pdf.length > 0;
|
2017-04-12 02:44:22 +00:00
|
|
|
},
|
2021-06-14 06:10:18 +00:00
|
|
|
availablePages() {
|
|
|
|
return this.pages.filter(Boolean);
|
|
|
|
},
|
2018-10-10 07:13:34 +00:00
|
|
|
},
|
|
|
|
watch: { pdf: 'load' },
|
|
|
|
mounted() {
|
2019-03-13 11:55:42 +00:00
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc;
|
2018-10-10 07:13:34 +00:00
|
|
|
if (this.hasPDF) this.load();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
load() {
|
|
|
|
this.pages = [];
|
|
|
|
return pdfjsLib
|
2019-07-28 05:53:24 +00:00
|
|
|
.getDocument({
|
|
|
|
url: this.document,
|
|
|
|
cMapUrl: '/assets/webpack/cmaps/',
|
|
|
|
cMapPacked: true,
|
|
|
|
})
|
2019-07-29 19:47:40 +00:00
|
|
|
.promise.then(this.renderPages)
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((pages) => {
|
2019-07-29 16:04:00 +00:00
|
|
|
this.pages = pages;
|
2021-12-08 00:15:00 +00:00
|
|
|
this.$emit('pdflabload', pages.length);
|
2019-07-29 16:04:00 +00:00
|
|
|
})
|
2020-12-23 21:10:24 +00:00
|
|
|
.catch((error) => {
|
2019-07-29 16:04:00 +00:00
|
|
|
this.$emit('pdflaberror', error);
|
2018-10-10 07:13:34 +00:00
|
|
|
});
|
2018-01-05 00:18:35 +00:00
|
|
|
},
|
2018-10-10 07:13:34 +00:00
|
|
|
renderPages(pdf) {
|
|
|
|
const pagePromises = [];
|
|
|
|
for (let num = 1; num <= pdf.numPages; num += 1) {
|
2019-07-29 16:04:00 +00:00
|
|
|
pagePromises.push(pdf.getPage(num));
|
2018-10-10 07:13:34 +00:00
|
|
|
}
|
|
|
|
return Promise.all(pagePromises);
|
2017-04-12 02:44:22 +00:00
|
|
|
},
|
2018-10-10 07:13:34 +00:00
|
|
|
},
|
|
|
|
};
|
2017-04-12 02:44:22 +00:00
|
|
|
</script>
|
|
|
|
|
2017-10-04 00:07:41 +00:00
|
|
|
<template>
|
2018-11-16 20:07:38 +00:00
|
|
|
<div v-if="hasPDF" class="pdf-viewer">
|
2021-06-14 06:10:18 +00:00
|
|
|
<page v-for="(page, index) in availablePages" :key="index" :page="page" :number="index + 1" />
|
2017-10-04 00:07:41 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2017-04-12 02:44:22 +00:00
|
|
|
<style>
|
2018-10-10 07:13:34 +00:00
|
|
|
.pdf-viewer {
|
|
|
|
background: url('./assets/img/bg.gif');
|
|
|
|
display: flex;
|
|
|
|
flex-flow: column nowrap;
|
|
|
|
}
|
2017-04-12 02:44:22 +00:00
|
|
|
</style>
|