gitlab-org--gitlab-foss/app/assets/javascripts/pdf/index.vue

78 lines
1.7 KiB
Vue
Raw Normal View History

2017-04-11 22:44:22 -04:00
<script>
import pdfjsLib from 'pdfjs-dist/build/pdf';
import workerSrc from 'pdfjs-dist/build/pdf.worker.min';
2017-04-11 22:44:22 -04:00
import page from './page/index.vue';
2017-04-11 22:44:22 -04:00
export default {
components: { page },
props: {
pdf: {
type: [String, Uint8Array],
required: true,
2017-04-11 22:44:22 -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
},
hasPDF() {
return this.pdf && this.pdf.length > 0;
2017-04-11 22:44:22 -04:00
},
availablePages() {
return this.pages.filter(Boolean);
},
},
watch: { pdf: 'load' },
mounted() {
pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc;
if (this.hasPDF) this.load();
},
methods: {
load() {
this.pages = [];
return pdfjsLib
.getDocument({
url: this.document,
cMapUrl: '/assets/webpack/cmaps/',
cMapPacked: true,
})
.promise.then(this.renderPages)
.then((pages) => {
this.pages = pages;
this.$emit('pdflabload', pages.length);
})
.catch((error) => {
this.$emit('pdflaberror', error);
});
2018-01-04 19:18:35 -05:00
},
renderPages(pdf) {
const pagePromises = [];
for (let num = 1; num <= pdf.numPages; num += 1) {
pagePromises.push(pdf.getPage(num));
}
return Promise.all(pagePromises);
2017-04-11 22:44:22 -04:00
},
},
};
2017-04-11 22:44:22 -04:00
</script>
<template>
2018-11-16 15:07:38 -05:00
<div v-if="hasPDF" class="pdf-viewer">
<page v-for="(page, index) in availablePages" :key="index" :page="page" :number="index + 1" />
</div>
</template>
2017-04-11 22:44:22 -04:00
<style>
.pdf-viewer {
background: url('./assets/img/bg.gif');
display: flex;
flex-flow: column nowrap;
}
2017-04-11 22:44:22 -04:00
</style>