vue3+vite在线预览pdf

效果图

代码

javascript 复制代码
<template>
    <div class="pdf-preview">
        <div class="pdf-wrap">
            <vue-pdf-embed :source="state.source" :style="scale" class="vue-pdf-embed" :page="state.pageNum" />
        </div>
        <div class="page-tool">
            <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>
            <div class="page-tool-item" @click="$emit('close')">关闭</div>
        </div>
    </div>
</template>
<script setup lang="ts">
import VuePdfEmbed from "vue-pdf-embed";
//import { createLoadingTask } from "vue3-pdfjs/esm/vue-pdf.js";
import * as pdfjsLib from "pdfjs-dist";
import { reactive, onMounted, computed } from "vue";

const props = defineProps({
    pdfUrl: {
        type: String,
        required: true
    }
})


const state = reactive({
    source: props.pdfUrl, //预览pdf文件地址
    pageNum: 1, //当前页面
    scale: 1, // 缩放比例
    numPages: 0, // 总页数
});
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 > 0.8) {
        state.scale -= 0.1;
    }
}
onMounted(() => {
    pdfjsLib.GlobalWorkerOptions.workerSrc = "./pdf.worker.js";
    const loadingTask = pdfjsLib.getDocument(state.source);
    loadingTask.promise.then((pdf) => {
        state.numPages = pdf.numPages;
    });
});

</script>
<style lang="css" scoped>
.pdf-preview {
    position: fixed;
    height: 100vh;
    width: 100vw;
    padding: 20px 0;
    box-sizing: border-box;
    background-color: #e9e9e9;
    left: 0;
    top: 0;
    z-index: 99999999;
    overflow-y: auto;
}

.pdf-wrap {
    overflow-y: auto;
}

.vue-pdf-embed {
    text-align: center;
    width: 60vw;
    border: 1px solid #e5e5e5;
    margin: 0 auto;
    box-sizing: border-box;
}

.page-tool {
    position: fixed;
    bottom: 35px;
    padding-left: 15px;
    padding-right: 15px;
    display: flex;
    align-items: center;
    background: rgb(66, 66, 66);
    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;
    color: #fff !important;
}
</style>
相关推荐
张萌杰1 天前
深度学习的基础知识(常见名词解释)
人工智能·深度学习·机器学习·1024程序员节
开开心心就好2 天前
免费无广告卸载工具,轻便安全适配全用户
linux·运维·服务器·网络·安全·启发式算法·1024程序员节
开开心心就好3 天前
图片格式转换工具,右键菜单一键转换简化
linux·运维·服务器·python·django·pdf·1024程序员节
徐子童5 天前
网络协议---TCP协议
网络·网络协议·tcp/ip·面试题·1024程序员节
扫地的小何尚7 天前
NVIDIA RTX PC开源AI工具升级:加速LLM和扩散模型的性能革命
人工智能·python·算法·开源·nvidia·1024程序员节
数据皮皮侠AI8 天前
上市公司股票名称相似度(1990-2025)
大数据·人工智能·笔记·区块链·能源·1024程序员节
开开心心就好8 天前
系统清理工具清理缓存日志,启动卸载管理
linux·运维·服务器·神经网络·cnn·pdf·1024程序员节
Evan东少11 天前
[踩坑]笔记本Ubuntu20.04+NvidiaRTX5060驱动+cuda+Pytorch+ROS/Python实现人脸追踪(环境准备)
1024程序员节
不爱编程的小陈12 天前
C/C++每日面试题
面试·职场和发展·1024程序员节
开开心心就好12 天前
右键菜单管理工具,添加程序自定义名称位置
linux·运维·服务器·ci/cd·docker·pdf·1024程序员节