Table of Contents
Create vue3 project
Add PDF Preview plug-in
Home Web Front-end Vue.js How Vue3+Vue-PDF implements online preview of PDF files

How Vue3+Vue-PDF implements online preview of PDF files

May 13, 2023 pm 10:04 PM
vue3 pdf

Create vue3 project

We first create a Vue3 project, enter the command

pnpm create vite vue-pdf-preview
Copy after login

in the terminal, select vue-ts and press Enter , cd to enter the project root directory, execute pnpm install, and wait for the project dependency package to be installed.

After the project dependency package is installed, let’s start the project and execute the command pnpm run dev. You can see that the following content is entered on the console

  vite v2.9.9 dev server running at:
  > Local: http://localhost:3000/
  > Network: use `--host` to expose
  ready in 780ms.
Copy after login

Hold down control/command left mouse button, the project was opened in the browser

How Vue3+Vue-PDF implements online preview of PDF files

Project started successfully

Add PDF Preview plug-in

After the project is successfully started, we install the PDF preview plug-in

pnpm install vue-pdf-embed
pnpm install vue3-pdfjs
Copy after login

We create a new file under src src/ components/pdfPreview.vue, add some code to initialize the vue-pdf preview, the code is as follows

<template>
    <div class="pdf-preview">

    </div>
</template>
<script setup lang="ts">
import { reactive, onMounted, computed } from "vue";

const props = defineProps({
    pdfUrl: {
        type: String,
        required: true
    }
})
onMounted(() => {
});
</script>
<style lang="css" scoped>
.pdf-preview {
    position: relative;
    height: 100vh;
    padding: 20px 0;
    box-sizing: border-box;
    background: rgb(66, 66, 66);
}
.vue-pdf-embed {
    text-align: center;
    width: 515px;
    border: 1px solid #e5e5e5;
    margin: 0 auto;
    box-sizing: border-box;
}
</style>
Copy after login

After the addition is completed, we will introduce the PDF preview component Go to the App.vue file, and import the prepared PDF file, as shown below:

<template>
<div>
    <PDFView :pdfUrl="jsPdf" />
</div>
</template>
<script setup lang="ts">
import PDFView from "./components/pdfPreview.vue"
import jsPdf from "./Javascript.pdf"
</script>
Copy after login

Next we go back Go to the newly created PDF preview component page to improve the preview function

We first introduce the PDF preview plug-in:

import VuePdfEmbed from "vue-pdf-embed";
import { createLoadingTask } from "vue3-pdfjs/esm"; // 获得总页数
Copy after login

Use vue3 reactive Define some page number, page number, PDF file preview address variables

const state = reactive({
    source: props.pdfUrl, 预览pdf文件地址
    pageNum: 1, 当前页面
    scale: 1, // 缩放比例
    numPages: 0, // 总页数
});
Copy after login

Use createLoadingTask in the OnMounted hook function to obtain Download the total number of pages of the preview file

 const loadingTask = createLoadingTask(state.source);
    loadingTask.promise.then((pdf:{numPages: number}) => {
        state.numPages = pdf.numPages;
    });
Copy after login

Add the preview plug-in code to the template:

 <div class="pdf-wrap">
     <vue-pdf-embed :source="state.source"  : class="vue-pdf-embed" :page="state.pageNum" />
</div>
Copy after login

Open the browser and you can see that the PDF file has been loaded, but The style is not very good-looking. We will optimize the style in the next step and improve the page turning function, zoom function, and current page/total number of pages display function of the PDF file

Add the following code to tempate

<div class="page-tool">
    <div class="page-tool-item" >上一页</div>
    <div class="page-tool-item">下一页</div>
    <div class="page-tool-item">{{state.pageNum}}/{{state.numPages}}</div>
    <div class="page-tool-item" >放大</div>
    <div class="page-tool-item">缩小</div>
</div>
Copy after login

Beautification style:

.pdf-preview {
    position: relative;
    height: 100vh;
    padding: 20px 0;
    box-sizing: border-box;
    background-color: e9e9e9;
}
.pdf-wrap{
    overflow-y:auto ;
}
.vue-pdf-embed {
    text-align: center;
    width: 515px;
    border: 1px solid #e5e5e5;
    margin: 0 auto;
    box-sizing: border-box;
}

.page-tool {
    position: absolute;
    bottom: 35px;
    padding-left: 15px;
    padding-right: 15px;
    display: flex;
    align-items: center;
    background: rgb(66, 66, 66);
    color: white;
    border-radius: 19px;
    z-index: 100;
    cursor: pointer;
    margin-left: 50%;
    transform: translateX(-50%);
}
.page-tool-item {
    padding: 8px 15px;
    padding-left: 10px;
    cursor: pointer;
}
Copy after login

Add the page turning function of the file, the zoom function, the current page/total number of pages display function have been added and improved

const scale = computed(() => `transform:scale(${state.scale})`)
function lastPage() {
    if (state.pageNum > 1) {
        state.pageNum -= 1;
    }
}
function nextPage() {
    if (state.pageNum < state.numPages) {
        state.pageNum += 1;
    }
}
function pageZoomOut() {
    if (state.scale < 2) {
        state.scale += 0.1;
    }
}
function pageZoomIn() {
    if (state.scale > 1) {
        state.scale -= 0.1;
    }
}
Copy after login

tempalte

<div class="page-tool-item" @click="lastPage">上一页</div>
<div class="page-tool-item" @click="nextPage">下一页</div>
<div class="page-tool-item">{{state.pageNum}}/{{state.numPages}}</div>
<div class="page-tool-item" @click="pageZoomOut">放大</div>
<div class="page-tool-item" @click="pageZoomIn">缩小</div>
Copy after login

The above is the detailed content of How Vue3+Vue-PDF implements online preview of PDF files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to merge PDFs on iPhone How to merge PDFs on iPhone Feb 02, 2024 pm 04:05 PM

When working with multiple documents or multiple pages of the same document, you may want to combine them into a single file to share with others. For easy sharing, Apple allows you to merge multiple PDF files into one file to avoid sending multiple files. In this post, we will help you know all the ways to merge two or more PDFs into one PDF file on iPhone. How to Merge PDFs on iPhone On iOS, you can merge PDF files into one in two ways – using the Files app and the Shortcuts app. Method 1: Using Files App The easiest way to merge two or more PDFs into one file is to use the Files app. Open on iPhone

3 Ways to Get Text from PDF on iPhone 3 Ways to Get Text from PDF on iPhone Mar 16, 2024 pm 09:20 PM

Apple's Live Text feature recognizes text, handwritten notes and numbers in photos or through the Camera app and allows you to paste that information onto any other app. But what to do when you're working with a PDF and want to extract text from it? In this post, we will explain all the ways to extract text from PDF files on iPhone. How to Get Text from PDF File on iPhone [3 Methods] Method 1: Drag Text on PDF The easiest way to extract text from PDF is to copy it, just like on any other app with text . 1. Open the PDF file you want to extract text from, then long press anywhere on the PDF and start dragging the part of the text you want to copy. 2

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

How to process PDF files using PHP How to process PDF files using PHP Jun 19, 2023 pm 02:41 PM

As a universal file format, PDF files are widely used in various application scenarios, such as e-books, reports, contracts, etc. During the development process, we often need to generate, edit, read and other operations on PDF files. As a scripting language, PHP can also easily complete these tasks. This article will introduce how to use PHP to process PDF files. 1. Generate PDF files There are many ways to generate PDF files, the most common of which is to use the PDF library. PDF library is a tool that generates PDF documents for

How to convert pdg files to pdf How to convert pdg files to pdf Nov 14, 2023 am 10:41 AM

Methods include: 1. Use professional document conversion tools; 2. Use online conversion tools; 3. Use virtual printers.

How to import and annotate PDFs in Apple Notes How to import and annotate PDFs in Apple Notes Oct 13, 2023 am 08:05 AM

In iOS 17 and MacOS Sonoma, Apple added the ability to open and annotate PDFs directly in the Notes app. Read on to find out how it's done. In the latest versions of iOS and macOS, Apple has updated the Notes app to support inline PDFs, which means you can insert PDFs into Notes and then read, annotate, and collaborate on the document. This feature also works with scanned documents and is available on both iPhone and iPad. Annotate a PDF in Notes on iPhone and iPad If you're using an iPhone and want to annotate a PDF in Notes, the first thing to do is select the PDF file

How to export xmind files to pdf files How to export xmind files to pdf files Mar 20, 2024 am 10:30 AM

xmind is a very practical mind mapping software. It is a map form made using people's thinking and inspiration. After we create the xmind file, we usually convert it into a pdf file format to facilitate everyone's dissemination and use. Then How to export xmind files to pdf files? Below are the specific steps for your reference. 1. First, let’s demonstrate how to export the mind map to a PDF document. Select the [File]-[Export] function button. 2. Select [PDF document] in the newly appeared interface and click the [Next] button. 3. Select settings in the export interface: paper size, orientation, resolution and document storage location. After completing the settings, click the [Finish] button. 4. If you click the [Finish] button

What should I do if part of the conversion from cad to pdf is not displayed? What should I do if part of the conversion from cad to pdf is not displayed? Jun 30, 2023 am 09:43 AM

Solution to the problem that part of the CAD to PDF conversion cannot be displayed: 1. Re-adjust the layer settings in CAD to ensure that the content in all layers is displayed correctly; 2. Use some professional software to convert the CAD file to ensure the clarity of the output file. and accuracy, and batch output saves time and improves efficiency; 3. Optimize CAD files and delete unnecessary layers and elements to reduce file size and complexity; 4. Use a PDF viewer that supports the current PDF version .

See all articles