vue 中使用 vue-pdf 显示PDF

<!DOCTYPE html>
<html lang="zh">

<head>
    <style>
        canvas {
            margin-bottom: 8px;
            box-shadow: 0 2px 8px 4px rgba(0, 0, 0, 0.1);
        }

        .app-header {
            padding: 16px;
            box-shadow: 0 2px 8px 4px rgba(0, 0, 0, 0.1);
            background-color: #555;
            color: #ddd;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .app-content {
            padding: 24px 16px;
        }

        .right {
            margin-bottom: 0;
        }

        .right input {
            margin-right: 5px;
        }

        .btn {
            margin: 0 5px;
        }

        .pages {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        [v-cloak]{
            display: none;
        }
    </style>

</head>

<body id="page-top" class="landing-page no-skin-config">
<div id="app">
    <div class="container xs-container">
        <div v-if="news.pdf">
            <div class="app-header">
                <template v-if="isLoading">
                    Loading...
                </template>

                <template v-else>
                    <span v-if="showAllPages">
                        {{ pageCount }} page(s)
                    </span>
                    <span v-else>
                        <button class="btn" :disabled="page <= 1" @click="page--"></button>
                        {{ page }} / {{ pageCount }}
                        <button class="btn" :disabled="page >= pageCount" @click="page++"></button>
                    </span>
                    <label class="right"><input v-model="showAllPages" type="checkbox">显示所有页面</label>
                </template>
            </div>

            <div class="app-content">
                <vue-pdf-embed ref="pdfRef" :source="news.pdf" :page="page" @rendered="handleDocumentRender"/>
            </div>
            <div class="pages">
                <span v-if="showAllPages">
                            {{ pageCount }} page(s)
                        </span>
                <span v-else>
                    <button class="btn" :disabled="page <= 1" @click="page--"></button>
                    {{ page }} / {{ pageCount }}
                    <button class="btn" :disabled="page >= pageCount" @click="page++"></button>
                </span>
            </div>
        </div>
    </div>
</div>
<script src="/public/vue/vue2.js"></script>
<script src="https://unpkg.com/vue-pdf-embed@1.0.4/dist/vue2-pdf-embed.js"></script>

<script>
    const app = new Vue({
        data() {
            return {
                news: {},
                isLoading: true,
                page: 1,
                pageCount: 1,
                showAllPages: false,
            }
        },
        components: {
            VuePdfEmbed: window.VuePdfEmbed,
        },
        watch: {
            showAllPages() {
                this.page = this.showAllPages ? null : 1
            },
        },
        methods: {
            getNewsDetail() {
                $.ajax({
                    url: `pdf 地址`,
                    success: (res) => {
                        this.news = res
                        document.title = res.title
                    }
                })
            },
            handleDocumentRender() {
                this.isLoading = false
                this.pageCount = this.$refs.pdfRef.pageCount
            }
        },
        created() {
            this.getNewsDetail()
        }
    })
    app.$mount('#app')
</script>

</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3是一种流行的JavaScript框架,用于构建用户界面。它提供了一种声明式的方式来构建可复用的组件,并且具有响应式的数据绑定和组件化的开发模式。 关于使用Vue 3实现PDF搜索高亮,你可以使用vue-pdf库来实现。vue-pdf是一个基于PDF.js的Vue组件,可以在Vue应用显示PDF文件。 首先,你需要安装vue-pdf库。可以通过npm或yarn来安装: ``` npm install vue-pdf ``` 或 ``` yarn add vue-pdf ``` 安装完成后,在你的Vue组件引入vue-pdf: ```javascript import { pdf } from 'vue-pdf' export default { components: { pdf }, data() { return { pdfFile: '/path/to/your/pdf/file.pdf', searchQuery: '' } }, methods: { highlightText() { // 在这里实现PDF搜索高亮逻辑 } } } ``` 然后,在模板使用vue-pdf组件来显示PDF文件: ```html <template> <div> <pdf :src="pdfFile"></pdf> <input type="text" v-model="searchQuery" @input="highlightText" placeholder="搜索关键字"> </div> </template> ``` 在上面的代码,我们通过`:src`属性将PDF文件路径传递给vue-pdf组件进行显示。然后,我们使用`v-model`指令将输入框的值绑定到`searchQuery`变量上,并通过`@input`事件监听输入框的变化,调用`highlightText`方法来实现搜索高亮的逻辑。 至于具体的PDF搜索高亮逻辑,你可以使用PDF.js提供的API来实现。你可以使用PDF.js提供的`find`方法来搜索关键字,并使用`highlight`方法来实现高亮效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值