2017-08-17 22:00:37 +05:30
|
|
|
<script>
|
2019-09-04 21:01:54 +05:30
|
|
|
import pdfjsLib from 'pdfjs-dist/build/pdf';
|
|
|
|
import workerSrc from 'pdfjs-dist/build/pdf.worker.min';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
import page from './page/index.vue';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
export default {
|
|
|
|
components: { page },
|
|
|
|
props: {
|
|
|
|
pdf: {
|
|
|
|
type: [String, Uint8Array],
|
|
|
|
required: true,
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
pages: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
document() {
|
|
|
|
return typeof this.pdf === 'string' ? this.pdf : { data: this.pdf };
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
hasPDF() {
|
|
|
|
return this.pdf && this.pdf.length > 0;
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
},
|
|
|
|
watch: { pdf: 'load' },
|
|
|
|
mounted() {
|
2019-04-03 18:18:56 +05:30
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc;
|
2018-12-13 13:39:08 +05:30
|
|
|
if (this.hasPDF) this.load();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
load() {
|
|
|
|
this.pages = [];
|
|
|
|
return pdfjsLib
|
|
|
|
.getDocument(this.document)
|
|
|
|
.then(this.renderPages)
|
|
|
|
.then(() => this.$emit('pdflabload'))
|
|
|
|
.catch(error => this.$emit('pdflaberror', error))
|
|
|
|
.then(() => {
|
|
|
|
this.loading = false;
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
renderPages(pdf) {
|
|
|
|
const pagePromises = [];
|
|
|
|
this.loading = true;
|
|
|
|
for (let num = 1; num <= pdf.numPages; num += 1) {
|
|
|
|
pagePromises.push(pdf.getPage(num).then(p => this.pages.push(p)));
|
|
|
|
}
|
|
|
|
return Promise.all(pagePromises);
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
},
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
</script>
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
<template>
|
2019-02-15 15:39:39 +05:30
|
|
|
<div v-if="hasPDF" class="pdf-viewer">
|
2018-03-17 18:26:18 +05:30
|
|
|
<page
|
|
|
|
v-for="(page, index) in pages"
|
|
|
|
:key="index"
|
|
|
|
:v-if="!loading"
|
|
|
|
:page="page"
|
|
|
|
:number="index + 1"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
<style>
|
2018-12-13 13:39:08 +05:30
|
|
|
.pdf-viewer {
|
|
|
|
background: url('./assets/img/bg.gif');
|
|
|
|
display: flex;
|
|
|
|
flex-flow: column nowrap;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
</style>
|