Introduction
Previewing PDF files is a common requirement in modern web applications. This article introduces a PDF preview component based on Vue3 and TypeScript that supports pagination preview, printing, and download capabilities.
Technology stack
- Vue3
- TypeScript
- Element Plus
- unocss
- vue-pdf-embed
Functional Features
- Pagination preview: Supports switching between different PDF pages.
- Print: Provides the ability to print PDFs directly in the browser.
- download: Users can download the entire PDF document.
- Show all pages: Provides an option where users can choose to view all pages.
Component code
Here is the core code of the component:
<script setup lang="ts"> import { ref } from "vue"; import VuePdfEmbed from "vue-pdf-embed"; const pdfUrl = "/mozilla//ba2edeae/web/"; const dialogVisible = ref(false); const loading = ref(false); const pdfRef = ref(); const source = ref(""); const currentPage = ref<number | undefined>(1); const pageCount = ref(1); const showAllPages = ref(false); const open = (url: string = pdfUrl) => { = url; = true; = true; }; const documentRender = () => { = false; = ; }; const showAllPagesChange = () => { = ? undefined : 1; }; const handleDownload = () => { (); }; const handlePrint = () => { // If the print page is twice as high as the print page when printing, the print configuration page settings are only available for pages with odd page numbers. (); }; defineExpose({ open }); </script> <template> <div> <el-dialog v-model="dialogVisible" title="Preview" width="80%" align-center destroy-on-close> <div flex="~ justify-between items-center"> <div> <el-pagination v-if="!showAllPages" v-model:current-page="currentPage" layout="prev, pager, next" :page-size="1" :total="pageCount" hide-on-single-page /> <div v-else>common{{ pageCount }}Page</div> </div> <div flex="~ items-center"> <el-checkbox v-model="showAllPages" @change="showAllPagesChange">Show all</el-checkbox> <el-tooltip effect="dark" content="download"> <SvgIcon ml-2 color="#000" cursor-pointer @click="handleDownload" :icon-style="{ width: '20px', height: '20px' }" name="download" /> </el-tooltip> <el-tooltip effect="dark" content="Print"> <SvgIcon ml-2 color="#000" cursor-pointer @click="handlePrint" :icon-style="{ width: '20px', height: '20px' }" name="printing" /> </el-tooltip> </div> </div> <el-scrollbar mt-3 height="75vh" v-loading="loading"> <vue-pdf-embed ref="pdfRef" container overflow-auto :source="source" :page="currentPage" @rendered="documentRender" /> </el-scrollbar> </el-dialog> </div> </template> <style lang="scss" scoped></style>
Summarize
By combining Vue3, TypeScript, and other modern front-end technologies, we have created a feature-rich PDF preview component. This component provides user-friendly paging preview, printing and downloading capabilities, providing developers with a convenient solution to integrating PDF previews in their web applications.
This is the article about Vue3+TypeScript implementing PDF preview components. For more related Vue3 TypeScript PDF preview content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!