In modern web applications, PDF documents are very common. Whether it’s a user manual, report, invoice or other types of documents, PDF is a great choice because it maintains a consistent look and format across a variety of devices and operating systems. In this article, we will explore how to embed PDF documents using the vue-pdf library in our application.
What is Vue-PDF
Vue-PDF is a component that allows you to embed PDF documents in Vue applications. It is based on Mozilla's library, a PDF rendering library written in JavaScript that can render PDF documents directly in the browser without any plug-ins or additional tools.
Install and use Vue-PDF
First, you need to install vue-pdf in your Vue project. You can use npm or yarn to install:
npm install vue-pdf # oryarn add vue-pdf
Then you can import and use vue-pdf in your Vue component:
<template> <div> <vue-pdf :src="pdfUrl" /> </div> </template> <script> import { VuePdf } from 'vue-pdf' export default { components: { VuePdf }, data() { return { pdfUrl: '/' } } } </script>
In this example, we use the :src attribute to specify the URL of the PDF document. When the component renders, it will load the PDF document from this URL and display it on the page.
Advanced features of Vue-PDF
In addition to the basic PDF embedding function, Vue-PDF also provides some advanced features that allow you to better control the display and interaction of PDF documents.
Pagination and navigation
Vue-PDF provides page and num-pages properties, allowing you to control the currently displayed page and total number of pages. You can use these properties to implement the paging and navigation functions of PDF documents:
<template> <div> <button @click="prevPage">Previous Page</button> <button @click="nextPage">Next Page</button> <vue-pdf :src="pdfUrl" :page="currentPage" @num-pages="totalPages = $event" /> <p>Page {{ currentPage }} of {{ totalPages }}</p> </div> </template> <script> import { VuePdf } from 'vue-pdf' export default { components: { VuePdf }, data() { return { pdfUrl: '/', currentPage: 1, totalPages: 0 } }, methods: { prevPage() { if ( > 1) { -- } }, nextPage() { if ( < ) { ++ } } } } </script>
In this example, we use the page attribute to control the currently displayed page, and use the num-pages event to get the total number of pages. We also added two buttons that users can click on to switch to the previous page or the next page.
Zoom and rotate
Vue-PDF also provides scale and rotate properties, allowing you to control the scaling and rotation of PDF documents:
<template> <div> <button @click="zoomIn">Zoom In</button> <button @click="zoomOut">Zoom Out</button> <button @click="rotate">Rotate</button> <vue-pdf :src="pdfUrl" :scale="scale" :rotate="rotate" /> </div> </template> <script> import { VuePdf } from 'vue-pdf' export default { components: { VuePdf }, data() { return { pdfUrl: '/', scale: 1, rotate: 0 } }, methods: { zoomIn() { += 0.1 }, zoomOut() { if ( > 0.1) { -= 0.1 } }, rotate() { = ( + 90) % 360 } } } </script>
In this example, we use the scale attribute to control the scaling of the PDF document and the rotate attribute to control the rotation of the PDF document. We also added three buttons that users can click to zoom in, out, or rotate PDF documents.
in conclusion
Vue-PDF is a powerful component that allows you to easily embed and control PDF documents in Vue applications. Whether you need to display user manuals, reports, invoices in your app, or other types of PDF documents, Vue-PDF is a very good choice.
This is the article about how to use Vue-PDF to embed PDF documents in the application. For more related Vue-PDF embed PDF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!