zoukankan      html  css  js  c++  java
  • react--pdf-lib给PDF添加水印兼容IE11

    import { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib';

    export async function modifyPdf(url, texts) {
      const text = `${texts}`;
      const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer());
      const pdfDoc = await PDFDocument.load(existingPdfBytes);
      const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
      const page = pdfDoc.getPages();
      const firstPage = page[0];
      const { height } = firstPage.getSize();
      page.forEach(item => {
        item.drawText(text, {
          x: 10,
          y: height - 100,
          size: 30,
          font: helveticaFont,
          color: rgb(0.82, 0.86, 0.89),
          rotate: degrees(45),
        });
        item.drawText(text, {
          x: 480,
          y: height - 65,
          size: 30,
          font: helveticaFont,
          color: rgb(0.82, 0.86, 0.89),
          rotate: degrees(45),
        });
        item.drawText(text, {
          x: 15,
          y: height - 200,
          size: 30,
          font: helveticaFont,
          color: rgb(0.82, 0.86, 0.89),
          rotate: degrees(45),
        });
      });
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        const blob = new Blob([await pdfDoc.save()], { type: 'application/pdf' });
        window.navigator.msSaveBlob(blob,'预览.pdf');
      } else {
        const pdfUrl = URL.createObjectURL(
          new Blob([await pdfDoc.save()], { type: 'application/pdf' }),
        );
        window.open(pdfUrl, '_blank');
      }
    }
  • 相关阅读:
    Git 基础笔记整理1
    学习开源中国客户端
    IOS项目中的细节处理,如更改状态栏等等
    网络编程
    当FileWriter没有flush的时候,不写入文件
    批量移动文件
    批量重命名文件
    替换一个文件内的某个字符
    遍历map和删除map中的一个entry
    ArrayList的遍历-转载
  • 原文地址:https://www.cnblogs.com/huangzhenhui/p/13595103.html
Copyright © 2011-2022 走看看