docx-populator

1.3.8 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i docx-populator

Weekly Downloads

1

Version

1.3.8

License

MIT

Unpacked Size

56.2 kB

Total Files

13

Last publish

Collaborators

  • dogusev
  • victornikitin
  • satoruf