mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
This PR is handles the changes required to support envelopes. The new envelope editor/signing page will be hidden during release. The core changes here is to migrate the documents and templates model to a centralized envelopes model. Even though Documents and Templates are removed, from the user perspective they will still exist as we remap envelopes to documents and templates.
61 lines
1.3 KiB
TypeScript
61 lines
1.3 KiB
TypeScript
import {
|
|
PDFCheckBox,
|
|
PDFDocument,
|
|
PDFDropdown,
|
|
PDFRadioGroup,
|
|
PDFTextField,
|
|
} from '@cantoo/pdf-lib';
|
|
|
|
export type InsertFormValuesInPdfOptions = {
|
|
pdf: Buffer;
|
|
formValues: Record<string, string | boolean | number>;
|
|
};
|
|
|
|
export const insertFormValuesInPdf = async ({ pdf, formValues }: InsertFormValuesInPdfOptions) => {
|
|
const doc = await PDFDocument.load(pdf);
|
|
|
|
const form = doc.getForm();
|
|
|
|
if (!form) {
|
|
return pdf;
|
|
}
|
|
|
|
for (const [key, value] of Object.entries(formValues)) {
|
|
try {
|
|
const field = form.getField(key);
|
|
|
|
if (!field) {
|
|
continue;
|
|
}
|
|
|
|
if (typeof value === 'boolean' && field instanceof PDFCheckBox) {
|
|
if (value) {
|
|
field.check();
|
|
} else {
|
|
field.uncheck();
|
|
}
|
|
}
|
|
|
|
if (field instanceof PDFTextField) {
|
|
field.setText(value.toString());
|
|
}
|
|
|
|
if (field instanceof PDFDropdown) {
|
|
field.select(value.toString());
|
|
}
|
|
|
|
if (field instanceof PDFRadioGroup) {
|
|
field.select(value.toString());
|
|
}
|
|
} catch (err) {
|
|
if (err instanceof Error) {
|
|
console.error(`Error setting value for field ${key}: ${err.message}`);
|
|
} else {
|
|
console.error(`Error setting value for field ${key}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return await doc.save().then((buf) => Buffer.from(buf));
|
|
};
|