docx-populator
Library for populating docx templates with json data
Last updated 3 months ago by dogusev .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install docx-populator 
SYNC missed versions from official npm registry.

docx-populator

Library for populating docx templates with json data

Usage

  1. add this module as dependency for you project

  2. Use it, where you need

    const { docxPopulator } = require('docx-populator');

  3. Call docxPopulator with next arguments:

    • inputFilePath - relative, or absolute path to .docx file
    • outputFilePath - path, where populated .docx should appear
    • dataObj - JSON object with data
    • isPdf - boolean, true if result should generate PDF file
  4. 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.

Current Tags

  • 1.3.6                                ...           latest (3 months ago)

18 Versions

  • 1.3.6                                ...           3 months ago
  • 1.3.5                                ...           3 months ago
  • 1.3.4                                ...           3 months ago
  • 1.3.3                                ...           3 months ago
  • 1.3.1                                ...           4 months ago
  • 1.3.0                                ...           5 months ago
  • 1.2.6                                ...           5 months ago
  • 1.2.5                                ...           5 months ago
  • 1.2.4                                ...           5 months ago
  • 1.2.3                                ...           5 months ago
  • 1.2.2                                ...           5 months ago
  • 1.2.1                                ...           5 months ago
  • 1.2.0                                ...           5 months ago
  • 1.1.2                                ...           6 months ago
  • 1.1.1                                ...           6 months ago
  • 1.1.0                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (9)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |