Vue3前端h5移动端页面预览PDF使用pdfjs-dist,添加自定义文本水印

本文主要是介绍Vue3前端h5移动端页面预览PDF使用pdfjs-dist,添加自定义文本水印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pdfjs-dist版本

pnpm i pdfjs-dist@2.5.207


<script setup>
import {ref, onMounted, watch} from 'vue'
import { useRoute } from "vue-router";
import * as pdfjsLib from 'pdfjs-dist'const route = useRoute()
// !
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.js', import.meta.url).href
const numPages = ref(0) // pdf一共多少页
const nums = ref(1) // 循环加载的参数
const currentNum = ref(1) // 当前页数
const jumpNum = ref(1)  // 输入框需要跳转的页数
const inpDisabled = ref(true) // pad文件没有加载完所有页数则禁用
const imgSrcList = ref([]) // 存储paf每一页
const watermarkPoint = [[-120, -66], [-120, 66], [120, -66], [120, 66]
] // 水印坐标
let pageHeight = null // 每一页高度
let pdfWrap = null
let isChangeCurrentNum = falseconst query = new URLSearchParams(route.fullPath.split('?')[1]) // 从url地址栏获取水印文本信息
const watermarkText = query.get('text') // 自定义的水印文本// url为pdf链接
const loadingTask = pdfjsLib.getDocument(query.get('url'))// dom加载之后
onMounted(() => {pdfWrap = document.getElementById('pdf')togglePage(nums.value)
})watch(() => nums.value, (num) => {if (num <= numPages.value) {togglePage(num)} else {inpDisabled.value = false}
})async function togglePage(pageNumber = 1) {loadingTask.promise.then(function(pdf) {numPages.value = pdf.numPagespdf.getPage(pageNumber).then((page) => {const scale = 0.1 // 关键!如果清晰度不行,慢慢调整这个数值。let viewport = page.getViewport({ scale });let scaleViewport = page.getViewport({ scale: window.screen.width / viewport.width });let canvas = document.createElement('canvas');let context = canvas.getContext('2d');canvas.width = scaleViewport.width;canvas.height = scaleViewport.height;// 只赋值一次	!pageHeight && (pageHeight = (scaleViewport.height * scale).toFixed(2))let renderContext = {canvasContext: context,viewport: scaleViewport,};let renderTask = page.render(renderContext);renderTask.promise.then(() => {// 设置自定义文本样式context.font = `${16 / scale}px Microsoft Yahei`;context.fillStyle = 'rgba(0, 0, 0, .1)'context.textAlign = 'center'context.textBaseline = 'middle'// 设置自定义文本位于每一页pdf的空间位置watermarkPoint.forEach(point => {context.translate( ((scaleViewport.width * scale / 2) + point[0]) / scale, ((scaleViewport.height * scale / 2) + point[1]) / scale )context.rotate(-30 * Math.PI / 180)context.fillText(watermarkText, 0, 0)context.resetTransform()})// 导出canvas图片到图片列表,循环渲染imgSrcList.value.push(canvas.toDataURL('img/png'))nums.value++});});}, function (reason) {console.error(reason);});
}function goToPage(num) {pdfWrap.scrollTo(0, num === 1 ? 0 : pageHeight * num - pageHeight)currentNum.value = numisChangeCurrentNum = true
}function handleScroll(e) {if (!isChangeCurrentNum) {const current = e.target.scrollTop / pageHeightcurrentNum.value = Math.ceil(current === 0 ? 1 : current)}isChangeCurrentNum = false
}function handleBeforePage() {currentNum.value = currentNum.value - 1 <= 0 ? 1 : currentNum.value - 1goToPage(currentNum.value)isChangeCurrentNum = true
}
function handleNextPage() {currentNum.value = currentNum.value + 1 >= numPages.value ? numPages.value : currentNum.value + 1goToPage(currentNum.value)isChangeCurrentNum = true
}
</script><template><div><div class="overflow-y-scroll" id="pdf" style="height: calc(100vh - 60px);" @scroll="handleScroll"><img v-for="src in imgSrcList" :src="src" alt=""></div><div v-if="imgSrcList.length !== 0" class="operation-box"><div class="operation"><div class="page-num-info"><span>{{ currentNum }}</span>/<span v-if="!inpDisabled">{{ numPages }}</span><van-loading style="margin-left: 3px;margin-top: 3px" v-else size="14" type="spinner" /></div><van-icon @click="handleBeforePage" name="arrow-left" /><div class="jump-box" v-if="!inpDisabled"><div class="inp-box">跳转 <input style="width: 60px;text-align: center;color: black" type="number" v-model="jumpNum" /></div><div @click.stop="goToPage(jumpNum)">确定</div></div><div v-else class="tips">文件正在加载中..跳转功能暂不可用</div><van-icon @click="handleNextPage" name="arrow" /><van-icon @click="goToPage(1)" class="home" name="wap-home" size="20" /></div></div></div>
</template><style scoped lang="less">
.operation-box {height: 60px;display: flex;align-items: center;justify-content: center;.operation {display: flex;align-items: center;justify-content: space-around;background-color: #404040;width: 100%;color: #fff;padding: 10px 20px;border-radius: 30px;box-sizing: border-box;.jump-box {flex: 1;font-size: 14px;display: flex;align-items: center;justify-content: center;.inp-box {margin-right: 5px;}}.tips {flex: 1;text-align: center;font-size: 14px;}.page-num-info {display: flex;justify-content: center;align-items: center;font-size: 14px;}.page-num-info, .home {width: 60px;text-align: center;}}
}
</style>

这篇关于Vue3前端h5移动端页面预览PDF使用pdfjs-dist,添加自定义文本水印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/619197

相关文章

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf