vue项目使用vue-pdf插件预览pdf文件

本文介绍了如何在Vue应用中安装并使用vue-pdf库,展示了一个名为PdfPreview的组件代码,包括PDF页面切换、缩放、旋转等交互功能的实现。
摘要由CSDN通过智能技术生成

1、安装vue-pdf:npm install --save vue-pdf
2、使用
具体实现代码:pdfPreview.vue

<template>
    <div class="container">
        <pdf
            ref="pdf"
            :src="pdfUrl"
            :page="currentPage"
            :rotate="pageRotate"
            class="pdf-box"
            @num-pages="pageCount = $event"
            @page-loaded="currentPage = $event"
            @loaded="loadPdfHandler"
        />
        <div class="tool-box">
            <el-button
                type="primary"
                circle
                icon="el-icon-caret-left"
                @click="changePdfPage(0)"
            />
            <span style="margin: 0 20px;">{{ currentPage }} / {{ pageCount }}</span>
            <el-button
                type="primary"
                circle
                icon="el-icon-caret-right"
                @click="changePdfPage(1)"
            />
            <el-button
                type="primary"
                circle
                icon="el-icon-zoom-in"
                @click="scaleD()"
            />
            <el-button
                type="primary"
                circle
                icon="el-icon-zoom-out"
                @click="scaleX()"
            />
            <el-button
                type="primary"
                circle
                icon="el-icon-refresh-left"
                @click="counterClock()"
            />
            <el-button
                type="primary"
                circle
                icon="el-icon-refresh-right"
                @click="clock()"
            />
        </div>
    </div>
</template>

<script>
import pdf from 'vue-pdf'
export default {
    name: 'PdfPreview',
    components: {
        pdf
    },
    props: {
        pdfUrl: {
            type: String,
            default: () => ''
        },
        sentData: {
            type: Object,
            default: () => {}
        }
    },
    data() {
        return {
            currentPage: 0, // pdf文件页码
            pageCount: 0, // pdf文件总页数
            scale: 100,
            pageRotate: 0,
            tempFileName: '',
            pdfContent: ''
        }
    },
    mounted() { },
    methods: {
    // pdf加载时
        loadPdfHandler(e) {
            e
            this.currentPage = 1 // 加载的时候先加载第一页
        },
        // 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
        changePdfPage(val) {
            if (val === 0 && this.currentPage > 1) {
                this.currentPage--
            }
            if (val === 1 && this.currentPage < this.pageCount) {
                this.currentPage++
            }
        },
        // 放大
        scaleD() {
            this.scale += 5
            this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
        },
        // 缩小
        scaleX() {
            if (this.scale === 100) {
                return
            }
            this.scale += -5
            this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
        },
        // 顺时针
        clock() {
            this.pageRotate += 90
        },
        // 逆时针
        counterClock() {
            this.pageRotate -= 90
        }
    }
}
</script>

<style lang="scss" scoped>
.container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: auto;

    img {
        position: absolute;
        right: 20px;
        bottom: 10px;
        width: 40px;
        cursor: pointer;
    }
}

.pdf-box {
    width: 100%;
    height: calc(100% - 56px);
    overflow: scroll;
}

.tool-box {
    position: absolute;
    bottom: 15px;
    left: 50%;
    margin-left: -164px;
}
</style>

调用:
在这里插入图片描述
效果图:
在这里插入图片描述

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值