1.3.8 • Published 7 months ago

docx-populator v1.3.8

Weekly downloads
85
License
MIT
Repository
github
Last release
7 months ago

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.
1.3.8

7 months ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago