docx-populator
v1.3.8
Published
Library for populating docx templates with json data
Downloads
7
Readme
docx-populator
Library for populating docx templates with json data
Usage
add this module as dependency for you project
Use it, where you need
const { docxPopulator } = require('docx-populator');
Call
docxPopulator
with next arguments:inputFilePath
- relative, or absolute path to .docx fileoutputFilePath
- path, where populated .docx should appeardataObj
- JSON object with dataisPdf
- boolean, true if result should generate PDF file
Promise will be returned
Usage example:
const handleDocx = (data, cb) => {
const operationId = new Date().getTime();
console.log('operationId', operationId);
const dataObj = data.data;
const docsPath = '/docs/' + operationId;
const dirPath = 'public' + docsPath;
fs.mkdir(dirPath, () => {
const filePath = dirPath + '/input.docx';
const isPdf = true;
const file = fs.createWriteStream(filePath);
const getFunction = data.template.match(/^https/) ? https.get : http.get;
const request = getFunction(data.template, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(() => {
return docxPopulator(
filePath,
path.join(__dirname,'../',dirPath , '/outputdocx.docx'),
dataObj,
isPdf
).then((response) => {
// {path: 'path/to/local/file'}
});
});
});
}).on('error', function(err) {
console.log('! fail to load file', err);
if (cb) cb(err.message);
});
});
};
Changelog
- 1.1.0 - added isPdf argument, to render result as PDF file
- 1.0.0 - Initial functionality
- 1.0.1 - Do not run functions when populating, minor fixes for splitted placeholders.