From d9652e1f87f6f78dd465030b29cecf98f97d682c Mon Sep 17 00:00:00 2001 From: Chandler Copeland Date: Sat, 21 Mar 2026 14:06:12 -0600 Subject: [PATCH] feat(11-02): preparePdf() gains agentSignatureData param and embeds at agent-sig fields - Add optional agentSignatureData: string | null = null as 5th parameter - Import PDFImage from @cantoo/pdf-lib for typed agentSigImage variable - Embed PNG once before field loop, store as agentSigImage - Replace agent-signature stub with drawImage at field.x/y/width/height --- .../src/lib/pdf/prepare-document.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/teressa-copeland-homes/src/lib/pdf/prepare-document.ts b/teressa-copeland-homes/src/lib/pdf/prepare-document.ts index 733a9ed..b2bae4c 100644 --- a/teressa-copeland-homes/src/lib/pdf/prepare-document.ts +++ b/teressa-copeland-homes/src/lib/pdf/prepare-document.ts @@ -1,4 +1,4 @@ -import { PDFDocument, StandardFonts, rgb } from '@cantoo/pdf-lib'; +import { PDFDocument, PDFImage, StandardFonts, rgb } from '@cantoo/pdf-lib'; import { readFile, writeFile, rename } from 'node:fs/promises'; import type { SignatureFieldData } from '@/lib/db/schema'; import { getFieldType } from '@/lib/db/schema'; @@ -23,12 +23,19 @@ export async function preparePdf( destPath: string, textFields: Record, sigFields: SignatureFieldData[], + agentSignatureData: string | null = null, ): Promise { const pdfBytes = await readFile(srcPath); const pdfDoc = await PDFDocument.load(pdfBytes); const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica); const pages = pdfDoc.getPages(); + // Embed agent signature image once — reused across all agent-sig fields + let agentSigImage: PDFImage | null = null; + if (agentSignatureData) { + agentSigImage = await pdfDoc.embedPng(agentSignatureData); + } + // 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(); @@ -136,7 +143,15 @@ export async function preparePdf( // No marker drawn — text content is provided via textFillData (separate pipeline) } else if (fieldType === 'agent-signature') { - // Skip — agent signature handled by Phase 11; no placeholder drawn here + if (agentSigImage) { + page.drawImage(agentSigImage, { + x: field.x, + y: field.y, + width: field.width, + height: field.height, + }); + } + // If no signature saved: the prepare route guards against this with 422 before calling preparePdf } }