2019-02-26 22:20:20 +00:00
|
|
|
// Example on how to use a template document
|
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
2018-09-25 23:46:55 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import { Document, ImportDotx, Packer, Paragraph } from "../build";
|
|
|
|
|
|
|
|
const importDotx = new ImportDotx();
|
|
|
|
const filePath = "./demo/dotx/template.dotx";
|
|
|
|
|
|
|
|
fs.readFile(filePath, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
throw new Error(`Failed to read file ${filePath}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
importDotx.extract(data).then((templateDocument) => {
|
2021-03-19 20:53:56 +00:00
|
|
|
const doc = new Document(
|
|
|
|
{
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
titlePage: templateDocument.titlePageIsDefined,
|
|
|
|
},
|
|
|
|
children: [new Paragraph("Hello World")],
|
|
|
|
},
|
|
|
|
],
|
2019-07-31 08:48:02 +01:00
|
|
|
},
|
2021-03-19 20:53:56 +00:00
|
|
|
{
|
|
|
|
template: templateDocument,
|
|
|
|
},
|
|
|
|
);
|
2018-09-25 23:46:55 +01:00
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2018-09-25 23:46:55 +01:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|