• vue3+vite在线预览pdf


    效果图
    在这里插入图片描述
    代码

    <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
    • 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
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
  • 相关阅读:
    mybatis之动态SQL
    探索生成式AI的未来:Chat与Agent的较量与融合
    Decoupled Contrastive Learning 论文解读和感想
    代码随想录算法训练营Day59 | 503.下一个更大元素II 42. 接雨水
    JavaScript:从基础到进阶的全面介绍
    项目部署之持续集成
    ubuntu 22.04 深度学习环境配置
    Flask框架——MongoEngine使用MongoDB数据库
    Tomcat项目启动报错
    家用无线路由器如何用网线桥接解决有些房间无线信号覆盖不好的问题(低成本)
  • 原文地址:https://blog.csdn.net/weixin_45685544/article/details/134004652