2019-02-26 22:20:20 +00:00
|
|
|
// Add image to table cell
|
2018-12-24 16:50:53 +00:00
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2019-07-31 08:48:02 +01:00
|
|
|
import { Document, Header, Media, Packer, Paragraph, Table } from "../build";
|
2018-12-24 16:50:53 +00:00
|
|
|
|
|
|
|
const doc = new Document();
|
|
|
|
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
|
|
|
|
|
2019-03-18 23:50:21 +00:00
|
|
|
const table = new Table({
|
|
|
|
rows: 2,
|
|
|
|
columns: 2,
|
|
|
|
});
|
2019-06-25 23:17:56 +01:00
|
|
|
table.getCell(1, 1).add(new Paragraph(image));
|
2018-12-24 16:50:53 +00:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
// Adding same table in the body and in the header
|
|
|
|
doc.addSection({
|
|
|
|
headers: {
|
|
|
|
default: new Header({
|
|
|
|
children: [table],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
children: [table],
|
|
|
|
});
|
2018-12-24 16:50:53 +00:00
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2018-12-24 16:50:53 +00:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|