Skip to content

Latest commit

 

History

History
124 lines (99 loc) · 2.71 KB

README.md

File metadata and controls

124 lines (99 loc) · 2.71 KB

Build Status Coverage Status js-standard-style Greenkeeper badge

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')
  }
})

License

MIT

Robohash image of generate-docx