You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having trouble generating the signature in the document. I’ve tried with both a PDF created from scratch using pdf-lib and an existing PDF in versions 1.7 and 1.4, but all attempts have failed. I keep getting the error "pdf.ref is not a function." Has anyone encountered this problem? I was successful with placeholder-plain, but I'm unable to get it to work with placeholder-pdfkit010, which is supposed to allow for a visible signature.
import{pdfkitAddPlaceholder,ReturnType,}from"@signpdf/placeholder-pdfkit010";import{P12Signer}from"@signpdf/signer-p12";importsignpdffrom"@signpdf/signpdf";import{FastifyInstance}from"fastify";importfsfrom"fs";import{PDFDocument,rgb}from"pdf-lib";importzfrom"zod";exportasyncfunctiongetSign(app: FastifyInstance){app.get("/sign/:certificateId",async(request,reply)=>{constgetSignBody=z.object({certificateId: z.string().uuid(),});const{ certificateId }=getSignBody.parse(request.params);constpdfBuffer=fs.readFileSync(`${__dirname}/../../../resources/${certificateId}.pdf`);constcertificateBuffer=fs.readFileSync(`${__dirname}/../../../resources/certificate.p12`);consttargetPath=`${__dirname}/../../../output/${certificateId}.pdf`;constpdfWithVisual=awaitaddVisual(pdfBuffer);constsigner=newP12Signer(certificateBuffer,{passphrase: process.env.CERT_PASSWORD,});constsignedPdf=awaitsignpdf.sign(pdfWithVisual,signer);fs.writeFileSync(targetPath,signedPdf);returnreply.status(200).send({ certificateId });});}asyncfunctionaddVisual(pdfBuffer: Buffer): Promise<Uint8Array>{constpdfDoc=awaitPDFDocument.load(pdfBuffer);constpages=pdfDoc.getPages();constfirstPage=pages[0];const{ width, height }=firstPage.getSize();constmargin=30;constpadding=10;constlabelDoc="Doc signature";constlabelName="Company name";constlabelDate="Data: 03/07/2024 19:22:23-0300";constimageBuffer=fs.readFileSync(`${__dirname}/../../../assets/govbr-logo.png`);consttextWidth=200;consttextHeight=20;constx=width-textWidth-margin;consty=height-textHeight-margin;firstPage.drawText(labelDoc,{
x,
y,size: 10,color: rgb(0,0,0),});firstPage.drawText(labelName,{
x,y: y-20,size: 10,color: rgb(0,0,0),});firstPage.drawText(labelDate,{
x,y: y-35,size: 10,color: rgb(0,0,0),});constpngImage=awaitpdfDoc.embedPng(imageBuffer);// Desenhar a imagem na primeira páginafirstPage.drawImage(pngImage,{x: x-65,y: y-40,width: 70,height: 30,});constvisualRect=[x-padding,y-padding,x+textWidth+padding,y+textHeight+padding,];constwidgetRect=topLeftToBottomLeft(visualRect,{ width, height });constrefs=pdfkitAddPlaceholder({pdf: pdfDoc,
pdfBuffer,reason: "Showing off.",contactInfo: "[email protected]",name: "Sign PDF",location: "The digital world.",signatureLength: 1612,
widgetRect,signingTime: newDate(),});// Manually end the refs(Object.keys(refs)as(keyofReturnType)[]).forEach((key)=>{refs[key].end();});constmodifiedPdfBuffer=awaitpdfDoc.save();returnmodifiedPdfBuffer;}functiontopLeftToBottomLeft(coords: number[],page: {width: number;height: number}): number[]{return[coords[0],// x1page.height-coords[1],// y1coords[2],// x2page.height-coords[3],// y2];}
The text was updated successfully, but these errors were encountered:
I'm having trouble generating the signature in the document. I’ve tried with both a PDF created from scratch using pdf-lib and an existing PDF in versions 1.7 and 1.4, but all attempts have failed. I keep getting the error "pdf.ref is not a function." Has anyone encountered this problem? I was successful with placeholder-plain, but I'm unable to get it to work with placeholder-pdfkit010, which is supposed to allow for a visible signature.
The text was updated successfully, but these errors were encountered: