# docx-populator

> Library for populating docx templates with json data

Latest version **1.3.8** (published 2023-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install docx-populator
pnpm add docx-populator
yarn add docx-populator
bun add docx-populator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.8 |
| Published | 2023-10-12 |
| First published | 2019-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 54.9 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Author | D. Gusev |
| Maintainers | dogusev, victornikitin |

## Links

- npm: https://www.npmjs.com/package/docx-populator
- Repository: https://github.com/Bpium/docx-populator
- Homepage: https://github.com/Bpium/docx-populator#readme
- Issues: https://github.com/Bpium/docx-populator/issues
- npm.io page: https://npm.io/package/docx-populator

## Dependencies (10)

- [sax](https://npm.io/package/sax.md) >=0.6.0
- [util](https://npm.io/package/util.md) ^0.12.5
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [rimraf](https://npm.io/package/rimraf.md) ^2.6.3
- [zip-dir](https://npm.io/package/zip-dir.md) ^1.0.2
- [docx-pdf](https://npm.io/package/docx-pdf.md) 0.0.1
- [temp-dir](https://npm.io/package/temp-dir.md) ^2.0.0
- [traverse](https://npm.io/package/traverse.md) ^0.6.6
- [decompress](https://npm.io/package/decompress.md) ^4.2.0
- [xmlbuilder](https://npm.io/package/xmlbuilder.md) ~9.0.1

## Recent versions

- 1.3.8 (latest) — 2023-10-12
- 1.3.6 — 2020-02-20
- 1.3.5 — 2020-02-19
- 1.3.4 — 2020-02-19
- 1.3.3 — 2020-02-19
- 1.3.1 — 2020-01-27
- 1.3.0 — 2019-12-26
- 1.2.6 — 2019-12-26
- 1.2.5 — 2019-12-22
- 1.2.4 — 2019-12-21
- 1.2.3 — 2019-12-21
- 1.2.2 — 2019-12-20
- 1.2.1 — 2019-12-20
- 1.2.0 — 2019-12-20
- 1.1.2 — 2019-12-10
- … 4 more at https://npm.io/package/docx-populator/versions

## README

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

---
_Source: https://npm.io/package/docx-populator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
