[vue-pdf-embed@1] Advanced usage example
Advanced usage example of embedding a PDF document into a Vue application using vue-pdf-embed. Learn more at https://npmjs.com/package/vue-pdf-embed
by hrynko
HTML
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue2-pdf-embed.js"></script>
<div id="app">
<div class="app-header">
<template v-if="isLoading">
Loading...
</template>
<template v-else>
<span v-if="showAllPages">
{{ pageCount }} page(s)
</span>
<span v-else>
<button :disabled="page <= 1" @click="page--">❮</button>
{{ page }} / {{ pageCount }}
<button :disabled="page >= pageCount" @click="page++">❯</button>
</span>
<label class="right">
<input v-model="showAllPages" type="checkbox">
Show all pages
</label>
</template>
</div>
<div class="app-content">
<vue-pdf-embed
ref="pdfRef"
:source="pdfSource"
:page="page"
@password-requested="handlePasswordRequest"
@rendered="handleDocumentRender"
/>
</div>
</div>
CSS
body {
margin: 0;
padding: 0;
background-color: #ccc;
}
.vue-pdf-embed > div {
margin-bottom: 8px;
box-shadow: 0 2px 8px 4px rgba(0, 0, 0, 0.1);
}
.app-header {
padding: 16px;
box-shadow: 0 2px 8px 4px rgba(0, 0, 0, 0.1);
background-color: #555;
color: #ddd;
}
.app-content {
padding: 24px 16px;
}
.right {
float: right;
}
JavaScript
new Vue({
el: '#app',
components: {
VuePdfEmbed: window.VuePdfEmbed,
},
data() {
return {
isLoading: true,
page: null,
pageCount: 1,
pdfSource: 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf',
showAllPages: true,
}
},
watch: {
showAllPages() {
this.page = this.showAllPages ? null : 1
},
},
methods: {
handleDocumentRender() {
this.isLoading = false
this.pageCount = this.$refs.pdfRef.pageCount
},
handlePasswordRequest(callback, retry) {
callback(prompt(retry
? 'Enter password again'
: 'Enter password'
))
},
}
})