npm i vue-pdf-embed
App.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<template> <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"> <input type="file" accept=".pdf" @change="handleFileSelect" /> <vue-pdf-embed ref="pdfRef" :source="pdfSource" :page="page" @rendered="handleDocumentRender" /> </div> </template> <script> import VuePdfEmbed from 'vue-pdf-embed' export default { components: { VuePdfEmbed }, data() { return { isLoading: true, page: 1, pageCount: 1, pdfSource: null, // Initially null, will be set when file is selected showAllPages: true } }, watch: { showAllPages() { this.page = this.showAllPages ? null : 1 } }, methods: { handleDocumentRender(args) { console.log(args) this.isLoading = false this.pageCount = this.$refs.pdfRef.pageCount }, handleFileSelect(event) { const file = event.target.files[0] if (file && file.type === 'application/pdf') { const reader = new FileReader() reader.onload = () => { this.pdfSource = reader.result // Set the PDF file content as the source } reader.readAsDataURL(file) // Read the file as a Data URL } else { alert('Please select a valid PDF file.') } } } } </script> |