debian-mirror-gitlab/app/assets/javascripts/pdf/index.vue

81 lines
1.6 KiB
Vue
Raw Normal View History

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 {
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
2019-10-12 21:52:04 +05:30
.getDocument({
url: this.document,
cMapUrl: '/assets/webpack/cmaps/',
cMapPacked: true,
})
.promise.then(this.renderPages)
2021-03-08 18:12:59 +05:30
.then((pages) => {
2019-09-30 21:07:59 +05:30
this.pages = pages;
this.$emit('pdflabload');
})
2021-03-08 18:12:59 +05:30
.catch((error) => {
2019-09-30 21:07:59 +05:30
this.$emit('pdflaberror', error);
2018-12-13 13:39:08 +05:30
});
2018-03-17 18:26:18 +05:30
},
2018-12-13 13:39:08 +05:30
renderPages(pdf) {
const pagePromises = [];
for (let num = 1; num <= pdf.numPages; num += 1) {
2019-09-30 21:07:59 +05:30
pagePromises.push(pdf.getPage(num));
2018-12-13 13:39:08 +05:30
}
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"
2019-09-30 21:07:59 +05:30
v-if="page"
2018-03-17 18:26:18 +05:30
:key="index"
: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>