位置: IT常识 - 正文

Vue使用pdf-lib为文件流添加水印并预览(vue显示pdf)

编辑:rootadmin
Vue使用pdf-lib为文件流添加水印并预览

推荐整理分享Vue使用pdf-lib为文件流添加水印并预览(vue显示pdf),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue3.0 pdf,vue实现pdf预览功能,vue pdf,vue 展示pdf文件内容,vue pdf,vue pdf,vue3 pdf,vue 使用pdf.js,内容如对您有帮助,希望把文章链接给更多的朋友!

之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vue使用vue-pdf实现PDF文件预览,使用pdfobject预览pdf。这次项目中又要预览pdf了,要求还要加水印,做的时候又发现了一种预览pdf的方式,这种方式我觉的更好一些,并且还有个要求就是添加水印,当然水印后端也是可以加的,但是后端说了一堆...反正就是要让前端做,在我看来就是借口、不想做,最近也不忙,那我就给他搞出来好了。下面来介绍一下 

首先预览pdf就很简单了,我们只需要通过window.URL.createObjectURL(new Blob(file))转为一个路径fileSrc后,再通过window.open(fileSrc)就可以了,window.open方法第二个参数默认就是打开一个新页签,这样就可以直接预览了,很方便!就是下面这样子:

并且右上角自动给我们提供了下载、打印等功能。 

但是要加上水印的话,可能会稍微复杂一点点,我也百度找了好多,发现好多都是在项目里直接预览的,也就是在当前页面或者一个div有个容器用来专门预览pdf的,然后水印的话也是appendChild到容器div中进行的。这不是我想要的,并且也跟我现在预览的方式不一样,所以我的思路就是如何给文件的那个二进制blob流上加上水印,这样预览的时候也是用这个文件流,以后不想预览了、直接下载也要水印也是很方便的。找来找去找到了pdf-lib库,然后就去https://www.npmjs.com/package/pdf-lib这里去看了下使用示例,看了两个例子,发现好像这个很合适哦,终于一波操作拿下了,这就是我想要的。

我这里添加水印共三种方式,第一种就是可以直接传入文本,将文本添加进去作为水印 ;第二种是将图片的ArrayBuffer传递进去,将图片作为水印;因为第一种方式直接传文本只能传英文,我传入汉字就报错了,npm官网好像也有写,这是不可避免的,所以才有了第三种方式,就是也是传入文本,不过我们通过canvas画出来,然后通过toDataURL转为base64路径,然后再通过XHR去加载该图片拿到图片的Blob,再调用Blob的arrayBuffer方法拿到buffer传递进去作为水印,其实第三种和第二种都是图片的形式,第三种会更灵活一些。下面上代码

1. 安装 npm i pdf-lib2. 引入 //我的需求里只用到这么多就够了,其他的按需引入import { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib';3. 添加水印使用  3.1 添加文本水印import { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib';// This should be a Uint8Array or ArrayBuffer// This data can be obtained in a number of different ways// If your running in a Node environment, you could use fs.readFile()// In the browser, you could make a fetch() call and use res.arrayBuffer()const existingPdfBytes = ...// Load a PDFDocument from the existing PDF bytesconst pdfDoc = await PDFDocument.load(existingPdfBytes)// Embed the Helvetica fontconst helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)// Get the first page of the documentconst pages = pdfDoc.getPages()const firstPage = pages[0]// Get the width and height of the first pageconst { width, height } = firstPage.getSize()// Draw a string of text diagonally across the first pagefirstPage.drawText('This text was added with JavaScript!', { x: 5, y: height / 2 + 300, size: 50, font: helveticaFont, color: rgb(0.95, 0.1, 0.1), rotate: degrees(-45),})// Serialize the PDFDocument to bytes (a Uint8Array)const pdfBytes = await pdfDoc.save()// For example, `pdfBytes` can be:// • Written to a file in Node// • Downloaded from the browser// • Rendered in an <iframe>3.2 添加图片文本 import { PDFDocument } from 'pdf-lib'// These should be Uint8Arrays or ArrayBuffers// This data can be obtained in a number of different ways// If your running in a Node environment, you could use fs.readFile()// In the browser, you could make a fetch() call and use res.arrayBuffer()const jpgImageBytes = ...const pngImageBytes = ...// Create a new PDFDocumentconst pdfDoc = await PDFDocument.create()// Embed the JPG image bytes and PNG image bytesconst jpgImage = await pdfDoc.embedJpg(jpgImageBytes)const pngImage = await pdfDoc.embedPng(pngImageBytes)// Get the width/height of the JPG image scaled down to 25% of its original sizeconst jpgDims = jpgImage.scale(0.25)// Get the width/height of the PNG image scaled down to 50% of its original sizeconst pngDims = pngImage.scale(0.5)// Add a blank page to the documentconst page = pdfDoc.addPage()// Draw the JPG image in the center of the pagepage.drawImage(jpgImage, { x: page.getWidth() / 2 - jpgDims.width / 2, y: page.getHeight() / 2 - jpgDims.height / 2, width: jpgDims.width, height: jpgDims.height,})// Draw the PNG image near the lower right corner of the JPG imagepage.drawImage(pngImage, { x: page.getWidth() / 2 - pngDims.width / 2 + 75, y: page.getHeight() / 2 - pngDims.height, width: pngDims.width, height: pngDims.height,})// Serialize the PDFDocument to bytes (a Uint8Array)const pdfBytes = await pdfDoc.save()// For example, `pdfBytes` can be:// • Written to a file in Node// • Downloaded from the browser// • Rendered in an <iframe>

canvas那个也是用的这个这个通过图片添加水印 

上面这些都是官网上给的一些示例,我当时看到上面这两个例子,灵感瞬间就来了,然后测试,测试成功没问题,就开始整理代码,封装。结合自己的业务需求和可以复用通用的思想进行封装。下面贴一下最终的成功

3.3 封装previewPdf.jsimport { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib';/** * 浏览器打开新页签预览pdf * blob(必选): pdf文件信息(Blob对象)【Blob】 * docTitle(可选): 浏览器打开新页签的title 【String】 * isAddWatermark(可选,默认为false): 是否需要添加水印 【Boolean】 * watermark(必选):水印信息 【Object: { type: string, text: string, image:{ bytes: ArrayBuffer, imageType: string } }】 * watermark.type(可选):类型 可选值:text、image、canvas * watermark.text(watermark.type为image时不填,否则必填):水印文本。注意:如果watermark.type值为text,text取值仅支持拉丁字母中的218个字符。详见:https://www.npmjs.com/package/pdf-lib * watermark.image(watermark.type为image时必填,否则不填):水印图片 * watermark.image.bytes:图片ArrayBuffer * watermark.image.imageType:图片类型。可选值:png、jpg * Edit By WFT */export default class PreviewPdf { constructor({ blob, docTitle, isAddWatermark = false, watermark: { type = 'text', text = 'WFT', image } }) { const _self = this if(!blob) { return console.error('[PDF Blob Is a required parameter]') } if(!isAddWatermark) { // 不添加水印 _self.preView(blob, docTitle) } else { let bytes,imageType if(type == 'image') { if(!image) { return console.error('["image" Is a required parameter]') } bytes = image.bytes imageType = image.imageType } const map = { 'text': _self.addTextWatermark.bind(_self), 'image': _self.addImageWatermark.bind(_self), 'canvas': _self.addCanvasWatermark.bind(_self) } blob.arrayBuffer().then(async buffer => { const existingPdfBytes = buffer const pdfDoc = await PDFDocument.load(existingPdfBytes) let params if(type == 'text') params = { pdfDoc, text, docTitle } if(type == 'image') params = { pdfDoc, bytes, imageType, docTitle } if(type == 'canvas') params = { pdfDoc, text, docTitle } map[type](params) }).catch(e => console.error('[Preview Pdf Error]:', e)) } } // 添加 Text 水印 async addTextWatermark({ pdfDoc, text, docTitle }) { // console.log(StandardFonts, 'StandardFonts-->>') // 字体 const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica) const pages = pdfDoc.getPages() for(let i = 0; i < pages.length; i++) { let page = pages[i] let { width, height } = page.getSize() for(let i = 0; i < 6; i++) { for(let j = 0; j < 6; j++) { page.drawText(text, { x: j * 100, y: height / 5 + i * 100, size: 30, font: helveticaFont, color: rgb(0.95, 0.1, 0.1), opacity: 0.2, rotate: degrees(-35), }) } } } // 序列化为字节 const pdfBytes = await pdfDoc.save() this.preView(pdfBytes, docTitle) } // 添加 image 水印 async addImageWatermark({ pdfDoc, bytes, imageType, docTitle }) { // 嵌入JPG图像字节和PNG图像字节 let image const maps = { 'jpg': pdfDoc.embedJpg.bind(pdfDoc), 'png': pdfDoc.embedPng.bind(pdfDoc) } image = await maps[imageType](bytes) // 将JPG图像的宽度/高度缩小到原始大小的50% const dims = image.scale(0.5) const pages = pdfDoc.getPages() for(let i = 0; i < pages.length; i++) { let page = pages[i] let { width, height } = page.getSize() for(let i = 0; i < 6; i++) { for(let j = 0; j < 6; j++) { page.drawImage(image, { x: width / 5 - dims.width / 2 + j * 100, y: height / 5 - dims.height / 2 + i * 100, width: dims.width, height: dims.height, rotate: degrees(-35) }) } } } // 序列化为字节 const pdfBytes = await pdfDoc.save() this.preView(pdfBytes, docTitle) } // 添加 canvas 水印 addCanvasWatermark({ pdfDoc, text, docTitle }) { // 旋转角度大小 const rotateAngle = Math.PI / 6; // labels是要显示的水印文字,垂直排列 let labels = new Array(); labels.push(text); const pages = pdfDoc.getPages() const size = pages[0].getSize() let pageWidth = size.width let pageHeight = size.height let canvas = document.createElement('canvas'); let canvasWidth = canvas.width = pageWidth; let canvasHeight = canvas.height = pageHeight; const context = canvas.getContext('2d'); context.font = "15px Arial"; // 先平移到画布中心 context.translate(pageWidth / 2, pageHeight / 2 - 250); // 在绕画布逆方向旋转30度 context.rotate(-rotateAngle); // 在还原画布的坐标中心 context.translate(-pageWidth / 2, -pageHeight / 2); // 获取文本的最大长度 let textWidth = Math.max(...labels.map(item => context.measureText(item).width)); let lineHeight = 15, fontHeight = 12, positionY, i i = 0, positionY = 0 while (positionY <= pageHeight) { positionY = positionY + lineHeight * 5 i++ } canvasWidth += Math.sin(rotateAngle) * (positionY + i * fontHeight) // 给canvas加上画布向左偏移的最大距离 canvasHeight = 2 * canvasHeight for (positionY = 0, i = 0; positionY <= canvasHeight; positionY = positionY + lineHeight * 5) { // 进行画布偏移是为了让画布旋转之后水印能够左对齐; context.translate(-(Math.sin(rotateAngle) * (positionY + i * fontHeight)), 0); for (let positionX = 0; positionX < canvasWidth; positionX += 2 * textWidth) { let spacing = 0; labels.forEach(item => { context.fillText(item, positionX, positionY + spacing); context.fillStyle = 'rgba(187, 187, 187, .8)'; // 字体颜色 spacing = spacing + lineHeight; }) } context.translate(Math.sin(rotateAngle) * (positionY + i * fontHeight), 0); context.restore(); i++ } // 图片的base64编码路径 let dataUrl = canvas.toDataURL('image/png'); // 使用Xhr请求获取图片Blob let xhr = new XMLHttpRequest(); xhr.open("get", dataUrl, true); xhr.responseType = "blob"; xhr.onload = res => { const imgBlob = res.target.response // 获取Blob图片Buffer imgBlob.arrayBuffer().then(async buffer => { const pngImage = await pdfDoc.embedPng(buffer) for(let i = 0; i < pages.length; i++) { pages[i].drawImage(pngImage) } // 序列化为字节 const pdfBytes = await pdfDoc.save() this.preView(pdfBytes, docTitle) }) } xhr.send(); } // 预览 preView(stream, docTitle) { const URL = window.URL || window.webkitURL; const href = URL.createObjectURL(new Blob([stream], { type: 'application/pdf;charset=utf-8' })) const wo = window.open(href) // 设置新打开的页签 document title let timer = setInterval(() => { if(wo.closed) { clearInterval(timer) } else { wo.document.title = docTitle } }, 500) }}3.4 调用使用 

我这里将上面文件放在src/utils下 

3.4.1  预览(添加文本水印)Vue使用pdf-lib为文件流添加水印并预览(vue显示pdf)

代码: 

// 引入import PreviewPdf from '@/utils/previewPdf'// script// 实例化进行添加水印 并预览// file.raw 是要预览的pdf文件流 Blobnew PreviewPdf({ blob: file.raw, docTitle: 'window.open docTitle', isAddWatermark: true, // 是否需要添加水印 watermark: { // watermark必填 里面可以不填 type: 'text', text: 'WFT' }})

效果:

3.4.2 预览(添加图片水印) 

 代码:

// 引入import PreviewPdf from '@/utils/previewPdf'// scriptconst watermarkImage = require('@/assets/img/watermark.png') // 水印图片let xhr = new XMLHttpRequest();xhr.open("get", watermarkImage, true);xhr.responseType = "blob";xhr.onload = function (res) { const imgBlob = res.target.response // 水印图片的Blob流 imgBlob.arrayBuffer().then(buffer => { //get arraybuffer // 添加水印 预览 new PreviewPdf({ blob: file.raw, docTitle: file.name, isAddWatermark: true, watermark: { type: 'image', image: { bytes: buffer, imageType: 'png' } } }) })}xhr.send();

效果:

3.4.3 预览(添加文本canvas绘制水印) 

 代码:

// 引入import PreviewPdf from '@/utils/previewPdf'// scriptnew PreviewPdf({ blob: file.raw, docTitle: file.name, isAddWatermark: true, watermark: { type: 'canvas', text: 'WFT-CANVAS' }})

效果: 

因为有些样式调的不太好,就我目前写的我更偏向使用canvas这个,当然都是可以使用的,样式都是可以调整的。 

注意:里面的属性 isAddWatermark 设置为false或者不传该字段将不添加水印,还有watermark这个字段是必须的,穿个空对象也行像watermark:{}这样,因为我上面类中构造方法将参数结构了,可以按需调整。

整体的封装使用就是上面这样子了, 希望可以帮到有需要的伙伴~~~

再给大家一个直接往某个dom元素里面添加水印的方法 

不传参数默认为整个body添加水印 

function waterMark(text = 'WFT', dom = document.body) { if (document.getElementById('waterMark')) return // 旋转角度大小 var rotateAngle = Math.PI / 6; // labels是要显示的水印文字,垂直排列 var labels = new Array(); labels.push(text); let pageWidth = dom.clientWidth let pageHeight = dom.clientHeight let canvas = document.createElement('canvas'); let canvasWidth = canvas.width = pageWidth; let canvasHeight = canvas.height = pageHeight; var context = canvas.getContext('2d'); context.font = "15px Arial"; // 先平移到画布中心 context.translate(pageWidth / 2, pageHeight / 2 - 250); // 在绕画布逆方向旋转30度 context.rotate(-rotateAngle); // 在还原画布的坐标中心 context.translate(-pageWidth / 2, -pageHeight / 2); // 获取文本的最大长度 let textWidth = Math.max(...labels.map(item => context.measureText(item).width)); let lineHeight = 15, fontHeight = 12, positionY, i i = 0, positionY = 0 while (positionY <= pageHeight) { positionY = positionY + lineHeight * 5 i++ } canvasWidth += Math.sin(rotateAngle) * (positionY + i * fontHeight) // 给canvas加上画布向左偏移的最大距离 canvasHeight = 2 * canvasHeight for (positionY = 0, i = 0; positionY <= canvasHeight; positionY = positionY + lineHeight * 5) { // 进行画布偏移是为了让画布旋转之后水印能够左对齐; context.translate(-(Math.sin(rotateAngle) * (positionY + i * fontHeight)), 0); for (let positionX = 0; positionX < canvasWidth; positionX += 2 * textWidth) { let spacing = 0; labels.forEach(item => { context.fillText(item, positionX, positionY + spacing); spacing = spacing + lineHeight; }) } context.translate(Math.sin(rotateAngle) * (positionY + i * fontHeight), 0); context.restore(); i++ } let dataUrl = canvas.toDataURL('image/png'); let waterMarkPage = document.createElement('div'); waterMarkPage.id = "waterMark" let style = waterMarkPage.style; style.position = 'fixed'; style.overflow = "hidden"; style.left = 0; style.top = 0; style.opacity = '0.4'; style.background = "url(" + dataUrl + ")"; style.zIndex = 999; style.pointerEvents = "none"; style.width = '100%'; style.height = '100vh'; dom.appendChild(waterMarkPage);}
本文链接地址:https://www.jiuchutong.com/zhishi/295330.html 转载请保留说明!

上一篇:黄石国家公园的美洲野牛,美国怀俄明州 (© Gerald Corsi/Getty Images)(黄石国家公园的英文翻译)

下一篇:对于<router-view>标签的理解(对于异步电动机国家标准规定3kw)

  • 荣耀X30max怎么关闭应用(荣耀x30max怎么关掉虚尼键盘)

    荣耀X30max怎么关闭应用(荣耀x30max怎么关掉虚尼键盘)

  • steam删除好友对方知道吗(steam删除好友对方列表还有吗)

    steam删除好友对方知道吗(steam删除好友对方列表还有吗)

  • 华为平板怎样录屏(华为平板怎样录屏幕视频)

    华为平板怎样录屏(华为平板怎样录屏幕视频)

  • caj文件怎么打开(华为平板caj文件怎么打开)

    caj文件怎么打开(华为平板caj文件怎么打开)

  • 微信查询名下账户网络异常(微信查询名下账户人脸识别安全吗)

    微信查询名下账户网络异常(微信查询名下账户人脸识别安全吗)

  • 怎么通过微信知道对方的更多信息(怎么通过微信知道抖音号)

    怎么通过微信知道对方的更多信息(怎么通过微信知道抖音号)

  • 一键修复WORD(一键修复0xc0000225)

    一键修复WORD(一键修复0xc0000225)

  • 10900x与9900k的区别(9900x和10900k)

    10900x与9900k的区别(9900x和10900k)

  • 飞猪是哪个公司旗下的(飞猪是谁家的)

    飞猪是哪个公司旗下的(飞猪是谁家的)

  • 怎么拒收消息(不删微信怎么拒收消息)

    怎么拒收消息(不删微信怎么拒收消息)

  • 三星1676硒鼓型号(三星ml1676硒鼓)

    三星1676硒鼓型号(三星ml1676硒鼓)

  • qq等级皇冠之后是什么(qq等级到了皇冠然后最后到哪个?)

    qq等级皇冠之后是什么(qq等级到了皇冠然后最后到哪个?)

  • 华为nova2有没有录屏功能(华为nova2有没有OTG功能)

    华为nova2有没有录屏功能(华为nova2有没有OTG功能)

  • 电脑按什么键关闭程序(电脑按什么键关声音)

    电脑按什么键关闭程序(电脑按什么键关声音)

  • 苹果手机如何设置微信密码锁屏(苹果手机如何设置小圆点快捷键)

    苹果手机如何设置微信密码锁屏(苹果手机如何设置小圆点快捷键)

  • excel中一个完整的函数包括什么(Excel中一个完整的函数包括哪些)

    excel中一个完整的函数包括什么(Excel中一个完整的函数包括哪些)

  • 怎么屏蔽腾讯看点(腾讯怎么屏蔽不想看的视频)

    怎么屏蔽腾讯看点(腾讯怎么屏蔽不想看的视频)

  • 捡的小米手环怎么解绑(捡的小米手环怎么充电)

    捡的小米手环怎么解绑(捡的小米手环怎么充电)

  • 表格样式简明型1在哪(表格样式简明型1长什么样)

    表格样式简明型1在哪(表格样式简明型1长什么样)

  • 算法的时间复杂度是(算法的时间复杂度取决于)

    算法的时间复杂度是(算法的时间复杂度取决于)

  • 三星g9650是什么版本(三星g9650是什么型号多少钱)

    三星g9650是什么版本(三星g9650是什么型号多少钱)

  • 抖音里点赞有什么用(抖音点赞有啥用啊)

    抖音里点赞有什么用(抖音点赞有啥用啊)

  • 万用表都有什么档位(万用表都有什么品牌)

    万用表都有什么档位(万用表都有什么品牌)

  • 苹果xs max美版和国行的区别(苹果xsmax美版和国行拍照有区别吗)

    苹果xs max美版和国行的区别(苹果xsmax美版和国行拍照有区别吗)

  • Windows7系统blender文字模型变碎块吹散的解决方法(blender不支持win7)

    Windows7系统blender文字模型变碎块吹散的解决方法(blender不支持win7)

  • 销项税额期末余额
  • 出口退税新政策报关费发票要怎么开
  • 转让不动产取得的收入
  • 城建税 申报表
  • 合并扣税项是什么科目
  • 出口企业免抵税额在增值税申报表填写附表5
  • 企业注销的时候未分配利润怎么处理
  • 每个月0申报,对企业有什么影响吗?
  • 契税晚交有什么影响
  • 进项税额抵扣一般多少
  • 外经证预缴税款之后剩下的税款交到哪呢
  • 三证合一 更新
  • 自产的产品无偿赠送职工
  • 预付房租收到发票后如何做账
  • 老板向公司借款用于公司经营
  • 预付款可以开专票吗
  • 监控维护需要什么经营范围
  • 总资产报酬率可以用净利润计算吗
  • 关于增值税若干问题的探讨
  • 全国失信人员信息
  • 建筑发票怎么抵税
  • 以前年度加计扣除的税
  • 普通发票单张限额
  • 学校维修维护费包括哪些
  • 固定资产报废账目
  • 合作方寄来的礼物能不能收?
  • 代开专票不满10万的要交附加吗?
  • 出口视同内销如何申报?
  • 申报个税是按哪个月的工资表
  • win11 zen2
  • 鸿蒙系统字体不太好看
  • Win10系统如何修改开机密码
  • 存货的毁损通过什么科目核算
  • 路由器wds桥接成功为什么不能上网
  • 年终双薪是底薪吗
  • 加里西亚省
  • 深度学习中的注意力机制模型及代码实现(SE Attention、CBAM Attention)
  • linux rm 命令
  • 哪些情况进项税不可以抵扣?
  • 应收股利的明细科目
  • java集合框架主要有
  • strippped
  • 织梦安装数据库一直连接失败
  • 织梦前台的菜单怎么换
  • 玉米 收购
  • 小微企业买商品房怎么买
  • 仓库出入库账本怎么做
  • 返还个人所得税怎么操作
  • sqlserver有实例吗
  • 增值税达不到起征期
  • 计提个税和缴纳个税金额不符的原因
  • 房地产企业销售房屋印花税税率
  • 中小创投企业收益分析
  • 受托代销商品款是什么意思
  • 材料采购发票未到
  • 支付给其他公司的借款属于什么现金流
  • 专用发票冲红有时间有时间限制吗
  • 现金日记账本月合计怎么划线
  • 账户验证费会计分录
  • 工业企业材料入库账务处理
  • 行为异常不能使用优惠
  • ghost还原文件
  • windows windows.old
  • win10系统怎么清理电脑垃圾
  • OS X10.10.5 Yosemite beta2发布 os x10.10.5yosemite beta2官网下载地址
  • cpu numa
  • 桌面任务栏消失怎么办
  • linux如何查看用户的信息
  • XP系统升级WIN7系统
  • window10升级不了
  • linux内核配置文件
  • js匿名函数作用域
  • c盘权限恢复默认设置
  • eclipse折叠代码块if else工具
  • jquery课程内容总结
  • 我的宁夏灵活就业缴费失败
  • 江西省税务局官网查询系统
  • 纳税申报期过了怎么处理
  • 进项名称和销项同一产品不同称呼
  • 房屋租赁税房东不承担怎么办理
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设