一酥夭、下載插件
npm install pdf-lib@1.17.1
二暂幼、在頁面中引入插件
import { PDFDocument, rgb, PageSizes } from 'pdf-lib'
三藐窄、在頁面中使用插件
const constitutionUrl = 'https://xxx.xxxx.pdf';
const constitutionPdfBytes = await fetch(constitutionUrl).then((res) =>
res.arrayBuffer()
);
const pdfDoc = await PDFDocument.create();
const usConstitutionPdf = await PDFDocument.load(constitutionPdfBytes);
for (let item of usConstitutionPdf.getPages()) {
const pages = pdfDoc.addPage();
let obj = await pdfDoc.embedPage(item);
pages.drawPage(obj, {
x: 0,
y: 0,
width: pages.getWidth(),
height: pages.getHeight()
});
pages.drawText('xxx', {
x: pages.getWidth() - 50,
y: 40,
size: 16,
color: rgb(0.46, 0.53, 0.6),
opacity: 1,
})
}
const pdfBytess = await pdfDoc.save()
四贮泞、下載添加了簽章的PDF文檔
let blobData = new Blob([pdfBytes], { type: "application/pdf;Base64" });
let a = document.createElement("a");
a.target = "_blank";
a.href = window.URL.createObjectURL(blobData);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);