huangzhenhui
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\');
  }
}

分类:

技术点:

相关文章:

  • 2021-06-15
  • 2021-08-06
  • 2021-12-15
  • 2021-09-07
  • 2021-12-15
猜你喜欢
  • 2021-12-29
  • 2021-12-15
  • 2021-12-15
  • 2021-12-15
  • 2021-08-06
  • 2021-12-14
相关资源
相似解决方案