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

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图