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

相关文章

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

自定义注解SpringBoot防重复提交AOP方法详解

《自定义注解SpringBoot防重复提交AOP方法详解》该文章描述了一个防止重复提交的流程,通过HttpServletRequest对象获取请求信息,生成唯一标识,使用Redis分布式锁判断请求是否... 目录防重复提交流程引入依赖properties配置自定义注解切面Redis工具类controller

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Python自动化提取多个Word文档的文本

《Python自动化提取多个Word文档的文本》在日常工作和学习中,我们经常需要处理大量的Word文档,本文将深入探讨如何利用Python批量提取Word文档中的文本内容,帮助你解放生产力,感兴趣的小... 目录为什么需要批量提取Word文档文本批量提取Word文本的核心技术与工具安装 Spire.Doc

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x