fix: Implement fillPdf function that was returning empty array

The fillPdf function was a stub that returned an empty Uint8Array.
Implemented proper PDF form filling using pdf-lib to handle both
text fields and checkboxes.

https://claude.ai/code/session_01SYhhaVHw6we1P6Y2kqLnDe
This commit is contained in:
Claude 2026-01-31 10:09:29 +00:00
parent 1eb0683684
commit 458d3f9b64
No known key found for this signature in database

View file

@ -100,5 +100,36 @@ export const createFilledPdf = async (base64: string, fields: ExtractedField[],
};
export const fillPdf = async (base64: string, fieldValues: Record<string, string | boolean>): Promise<Uint8Array> => {
return new Uint8Array();
const pdfDoc = await PDFDocument.load(base64);
try {
const form = pdfDoc.getForm();
for (const [fieldName, value] of Object.entries(fieldValues)) {
try {
const field = form.getField(fieldName);
if (!field) continue;
if (field instanceof PDFTextField) {
field.setText(String(value));
} else if (field instanceof PDFCheckBox) {
const isChecked = typeof value === 'boolean'
? value
: String(value).toLowerCase() === 'true' || String(value).toLowerCase() === 'yes';
if (isChecked) {
field.check();
} else {
field.uncheck();
}
}
} catch (e) {
// Field might be read-only or have other issues - continue with other fields
console.warn(`Could not fill field "${fieldName}":`, e);
}
}
} catch (e) {
console.warn("Error filling form fields:", e);
}
return await pdfDoc.save();
};