diff --git a/teressa-copeland-homes/src/lib/pdf/prepare-document.ts b/teressa-copeland-homes/src/lib/pdf/prepare-document.ts index b2bae4c..bb7c72d 100644 --- a/teressa-copeland-homes/src/lib/pdf/prepare-document.ts +++ b/teressa-copeland-homes/src/lib/pdf/prepare-document.ts @@ -24,6 +24,7 @@ export async function preparePdf( textFields: Record, sigFields: SignatureFieldData[], agentSignatureData: string | null = null, + agentInitialsData: string | null = null, ): Promise { const pdfBytes = await readFile(srcPath); const pdfDoc = await PDFDocument.load(pdfBytes); @@ -36,6 +37,12 @@ export async function preparePdf( agentSigImage = await pdfDoc.embedPng(agentSignatureData); } + // Embed agent initials image once — reused across all agent-initials fields + let agentInitialsImage: PDFImage | null = null; + if (agentInitialsData) { + agentInitialsImage = await pdfDoc.embedPng(agentInitialsData); + } + // Track which text field entries were successfully written via AcroForm so that // the fallback text stamp only shows entries that were NOT already embedded. const acroFilledKeys = new Set(); @@ -152,6 +159,17 @@ export async function preparePdf( }); } // If no signature saved: the prepare route guards against this with 422 before calling preparePdf + + } else if (fieldType === 'agent-initials') { + if (agentInitialsImage) { + page.drawImage(agentInitialsImage, { + x: field.x, + y: field.y, + width: field.width, + height: field.height, + }); + } + // If no initials saved: the prepare route guards against this with 422 before calling preparePdf } }