Files
docx-js/demo/demo27.ts

28 lines
827 B
TypeScript
Raw Normal View History

2018-08-29 18:36:48 +03:00
import * as fs from "fs";
2018-09-19 00:37:11 +01:00
import { ImportDocx, Packer, Paragraph, TemplatedDocument } from "../build";
2018-08-29 18:36:48 +03:00
2018-09-06 09:09:36 +01:00
const importDocx = new ImportDocx();
2018-09-04 17:16:31 +03:00
const filePath = "./demo/dotx/template.dotx";
2018-09-06 09:09:36 +01:00
2018-09-04 17:16:31 +03:00
fs.readFile(filePath, (err, data) => {
2018-08-29 18:36:48 +03:00
if (err) {
2018-09-06 09:09:36 +01:00
throw new Error(`Failed to read file ${filePath}.`);
2018-08-29 18:36:48 +03:00
}
2018-09-06 09:09:36 +01:00
importDocx.extract(data).then((templateDocument) => {
2018-09-06 09:18:52 +01:00
// This any needs fixing
2018-09-17 21:18:27 +01:00
const sectionProps = {
titlePage: true,
} as any;
2018-09-19 00:37:11 +01:00
const doc = new TemplatedDocument(templateDocument, undefined, sectionProps);
2018-09-06 09:09:36 +01:00
const paragraph = new Paragraph("Hello World");
doc.addParagraph(paragraph);
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
2018-08-29 18:36:48 +03:00
});
2018-09-06 09:09:36 +01:00
});
});