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