2021-03-18 02:48:37 +00:00
|
|
|
// The demo on the README.md
|
2019-12-31 15:49:34 +00:00
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2021-03-18 02:48:37 +00:00
|
|
|
import { Document, HeadingLevel, ImageRun, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "../build";
|
2019-12-31 15:49:34 +00:00
|
|
|
|
|
|
|
const table = new Table({
|
|
|
|
rows: [
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
2021-03-18 02:48:37 +00:00
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/image1.jpeg"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
2019-12-31 15:49:34 +00:00
|
|
|
verticalAlign: VerticalAlign.CENTER,
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
text: "Hello",
|
|
|
|
heading: HeadingLevel.HEADING_1,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
verticalAlign: VerticalAlign.CENTER,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
text: "World",
|
|
|
|
heading: HeadingLevel.HEADING_1,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
2021-03-18 02:48:37 +00:00
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/image1.jpeg"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
2019-12-31 15:49:34 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
const doc = new Document({
|
|
|
|
sections: [
|
|
|
|
{
|
2021-03-18 02:48:37 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
text: "Hello World",
|
|
|
|
heading: HeadingLevel.HEADING_1,
|
|
|
|
}),
|
|
|
|
table,
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/pizza.gif"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
2019-12-31 15:49:34 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|