2.1.1 • Published 5 years ago

generate-docx v2.1.1

Weekly downloads
130
License
MIT
Repository
github
Last release
5 years ago

Build Status Coverage Status js-standard-style

generate-docx

Generates .docx from template and data

Returns a Buffer or saves the generated file if given path and filename.

Example save file

Promises

const generateDocx = require('generate-docx')

const options = {
  template: {
    filePath: 'test/data/testdoc.docx',
    data: {
      title: 'This is the title',
      description: 'Description is good',
      body: 'My body is my temple'
    }
  },
  save: {
    filePath: 'test/data/savedfile.docx'
  }
}

generateDocx(options)
  .then(console.log)
  .catch(console.error)

Callback

const generateDocx = require('generate-docx')

const options = {
  template: {
    filePath: 'test/data/testdoc.docx',
    data: {
      title: 'This is the title',
      description: 'Description is good',
      body: 'My body is my temple'
    }
  },
  save: {
    filePath: 'test/data/savedfile.docx'
  }
}

generateDocx(options, (error, message) => {
  if (error) {
    console.error(error)
  } else {
    console.log(message)
  }
})

Example return buffer

Promises

const { writeFileSync } = require('fs')
const generateDocx = require('generate-docx')

const options = {
  template: {
    filePath: 'test/data/testdoc.docx',
    data: {
      title: 'This is the title',
      description: 'Description is good',
      body: 'My body is my temple'
    }
  }
}

generateDocx(options)
  .then(buf => {
    writeFileSync('test/data/frombuffer.docx', buf)
    console.log('File written')
  }).catch(console.error)

Callback

const { writeFileSync } = require('fs')
const generateDocx = require('generate-docx')

const options = {
  template: {
    filePath: 'test/data/testdoc.docx',
    data: {
      title: 'This is the title',
      description: 'Description is good',
      body: 'My body is my temple'
    }
  }
}

generateDocx(options, (error, buf) => {
  if (error) {
    console.error(error)
  } else {
    writeFileSync('test/data/frombuffer.docx', buf)
    console.log('File written')
  }
})

Options

If you need to pass an option object to configure docxtemplater you can do using templateOptions.
For example you can configure docxtemplater to parse \n as a linebreak in the document

const options = {
  template: {
    filePath: 'test/data/testdoc.docx',
    data: {
      title: 'This is the title',
      description: 'Description is good',
      body: 'My body is \n my temple'
    }
  },
  templateOptions: {
      linebreaks: true
  },
  save: {
    filePath: 'test/data/savedfile.docx'
  }
}

License

MIT

2.1.1

5 years ago

2.1.0

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago