前端为本地已生成的pdf文件添加水印

对于已存在的pdf需要如何添加水印呢?前端也可实现该功能。
利用pdf-lib库即可实现。
官网地址:https://pdf-lib.js.org/
源码地址:https://github.com/Hopding/pdf-lib
注意:如是添加图片水印,该水印图片需为网络图片

<html>
  <head>
    <meta charset="utf-8" />
    <script src="https://unpkg.com/pdf-lib@1.4.0"></script>
    <script src="https://unpkg.com/downloadjs@1.4.7"></script>
  </head>

  <body>
    <p>Click the button to modify an existing PDF document with <code>pdf-lib</code></p>
    <button onclick="modify()">Modify PDF</button>
    <p class="small">(Your browser will download the resulting file)</p>
  </body>
  <style>
  body {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  
  p {
    font-family: helvetica;
    font-size: 24px;
    text-align: center;
    margin: 25px;
  }
  
  .small {
    font-family: helvetica;
    font-size: 18px;
    text-align: center;
    margin: 25px;
  }
  
  button {
    background-color: #008CBA;
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    font-size: 16px;
  }
</style>
  <script>
    const { degrees, PDFDocument, rgb, StandardFonts } = PDFLib
   
    async function modify() {
      // Fetch an existing PDF document
      const url = 'xxx';
  		const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer())
      // Load a PDFDocument from the existing PDF bytes
      const pdfDoc = await PDFDocument.load(existingPdfBytes)
      // Embed the Helvetica font
      const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)
    //为第一页pdf添加文字水印
      // Get the first page of the document
      const pages = pdfDoc.getPages()
      //const firstPage = pages[0]
      // Get the width and height of the first page
      //const { width, height } = firstPage.getSize()
      // Draw a string of text diagonally across the first page
    //   firstPage.drawText('hello', {
    //     x: 5,
    //     y: height / 2 + 300,
    //     size: 50,
    //     font: helveticaFont,
    //     color: rgb(0.2, 0.1, 0.1),
    //     rotate: degrees(-45),
    //   })
      //为所有pdf添加图片水印
        const img ="http://xxx/水印.png";
        const ImgCover = await fetch(img).then((res) =>         res.arrayBuffer());
        const eleImgCover = await pdfDoc.embedPng(ImgCover);
         pages.forEach((item) => {
        item.drawImage(eleImgCover, {
          x: item.getWidth() / 2 - eleImgCover.width / 4,
          y: item.getHeight() / 2,
          width: eleImgCover.width/2,
          height: eleImgCover.height/2,
        });
      });
      const pdfBytes = await pdfDoc.save();
          download(pdfBytes,test.pdf, "application/pdf");
    }
  </script>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VIVI Xiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值