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中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash