-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const inputFileName = process.argv[2]; | ||
const jsonFileName = process.argv[3]; | ||
const outputFileName = process.argv[4]; | ||
|
||
const fs = require("fs"); | ||
var template = JSON.parse(fs.readFileSync(jsonFileName)); | ||
|
||
const PizZip = require("pizzip"); | ||
const Docxtemplater = require("docxtemplater"); | ||
|
||
const path = require("path"); | ||
|
||
// Load the docx file as binary content | ||
const content = fs.readFileSync( | ||
path.resolve(__dirname, inputFileName), | ||
"binary" | ||
); | ||
|
||
const zip = new PizZip(content); | ||
const expressionParser = require("docxtemplater/expressions.js"); | ||
|
||
const doc = new Docxtemplater(zip, { | ||
paragraphLoop: true, | ||
linebreaks: true, | ||
parser: expressionParser, | ||
}); | ||
|
||
doc.render(template); | ||
|
||
const buf = doc.getZip().generate({ | ||
type: "nodebuffer", | ||
// compression: DEFLATE adds a compression step. | ||
// For a 50MB output document, expect 500ms additional CPU time | ||
compression: "DEFLATE", | ||
}); | ||
|
||
// buf is a nodejs Buffer, you can either write it to a | ||
// file or res.send it with express for example. | ||
fs.writeFileSync(outputFileName, buf); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters