2023-03-31 00:13:36 +01:00
|
|
|
// Patch a document with patches
|
2023-06-05 00:33:43 +01:00
|
|
|
|
2023-03-31 00:13:36 +01:00
|
|
|
import * as fs from "fs";
|
2023-06-05 00:33:43 +01:00
|
|
|
import { IPatch, patchDocument, PatchType, TextRun } from "docx";
|
2023-03-31 00:13:36 +01:00
|
|
|
|
|
|
|
export const font = "Trebuchet MS";
|
|
|
|
export const getPatches = (fields: { [key: string]: string }) => {
|
|
|
|
const patches: { [key: string]: IPatch } = {};
|
|
|
|
|
|
|
|
for (const field in fields) {
|
|
|
|
patches[field] = {
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
children: [new TextRun({ text: fields[field], font })],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return patches;
|
|
|
|
};
|
|
|
|
|
|
|
|
const patches = getPatches({
|
|
|
|
salutation: "Mr.",
|
|
|
|
"first-name": "John",
|
|
|
|
});
|
|
|
|
|
2023-12-31 23:16:48 +00:00
|
|
|
patchDocument({
|
|
|
|
outputType: "nodebuffer",
|
|
|
|
data: fs.readFileSync("demo/assets/simple-template-3.docx"),
|
2023-03-31 00:13:36 +01:00
|
|
|
patches,
|
2023-12-31 23:16:48 +00:00
|
|
|
keepOriginalStyles: true,
|
2023-03-31 00:13:36 +01:00
|
|
|
}).then((doc) => {
|
|
|
|
fs.writeFileSync("My Document.docx", doc);
|
|
|
|
});
|