-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (56 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var JSZip = require('jszip');
var Docxtemplater = require('docxtemplater');
var expressions= require('angular-expressions');
var fs = require('fs');
var path = require('path');
// Load the docx file as a binary
var content = fs.readFileSync(path.resolve(__dirname, 'input.docx'), 'binary');
var angularParser = function(tag) {
return {
// This adds support for the . when using angular expressions
get: tag === '.' ? function(s){ return s;} : function(s) {
return expressions.compile(tag.replace(/’/g, "'"))(s);
}
};
};
var zip = new JSZip(content);
var doc = new Docxtemplater()
.loadZip(zip)
.setOptions({parser:angularParser})
.setData({
"users": [
{
"name": "John",
"num": 0
},
{
"name": "Mary",
"num": 6
},
{
"name": "Jane",
"num": 2
},
{
"name": "Sean",
"num": 3
}
]
});
try {
doc.render();
}
catch (error) {
var e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties,
}
console.log(JSON.stringify({error: e}));
// The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
throw error;
}
var buf = doc.getZip().generate({type: 'nodebuffer'});
// buf is a nodejs buffer, you can either write it to a file or do anything else with it.
fs.writeFileSync(path.resolve(__dirname, 'output.docx'), buf);