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
This commit is contained in:
Chandler Copeland
2026-03-21 14:06:12 -06:00
parent f1dbf45e37
commit d9652e1f87

View File

@@ -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<string, string>,
sigFields: SignatureFieldData[],
agentSignatureData: string | null = null,
): Promise<void> {
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<string>();
@@ -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
}
}