2026-01-21 04:16:23 +00:00
|
|
|
import { PDF } from '@libpdf/core';
|
2024-04-08 10:01:11 +00:00
|
|
|
|
|
|
|
|
export type InsertFormValuesInPdfOptions = {
|
|
|
|
|
pdf: Buffer;
|
|
|
|
|
formValues: Record<string, string | boolean | number>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const insertFormValuesInPdf = async ({ pdf, formValues }: InsertFormValuesInPdfOptions) => {
|
2026-01-21 04:16:23 +00:00
|
|
|
const doc = await PDF.load(pdf);
|
2024-04-08 10:01:11 +00:00
|
|
|
|
|
|
|
|
const form = doc.getForm();
|
|
|
|
|
|
|
|
|
|
if (!form) {
|
|
|
|
|
return pdf;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 04:16:23 +00:00
|
|
|
const filledForm = Object.entries(formValues).map(([key, value]) => [
|
|
|
|
|
key,
|
|
|
|
|
typeof value === 'boolean' ? value : value.toString(),
|
|
|
|
|
]);
|
2024-04-08 10:01:11 +00:00
|
|
|
|
2026-01-21 04:16:23 +00:00
|
|
|
form.fill(Object.fromEntries(filledForm));
|
2024-04-08 10:01:11 +00:00
|
|
|
|
2026-01-21 04:16:23 +00:00
|
|
|
return await doc.save({ incremental: true }).then((buf) => Buffer.from(buf));
|
2024-04-08 10:01:11 +00:00
|
|
|
};
|