docx-templates
Advanced tools
Comparing version 2.7.3 to 2.8.0
@@ -40,2 +40,3 @@ 'use strict'; | ||
var BUFFER_VALUE = 'buffer'; | ||
// ========================================== | ||
@@ -55,3 +56,3 @@ // Main | ||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(options) { | ||
var template, replaceImages, _probe, output, buffer, newOptions, imgDataBase64, imgNames, i, imgName, imgPath, imgBuf, report; | ||
var template, replaceImages, _probe, templateIsBuffer, output, buffer, newOptions, imgDataBase64, imgNames, i, imgName, imgPath, imgBuf, report, shouldOutputBuffer; | ||
@@ -63,3 +64,4 @@ return _regenerator2.default.wrap(function _callee$(_context) { | ||
template = options.template, replaceImages = options.replaceImages, _probe = options._probe; | ||
output = options.output || getDefaultOutput(template); | ||
templateIsBuffer = template instanceof Buffer; | ||
output = options.output || (templateIsBuffer ? BUFFER_VALUE : getDefaultOutput(template.toString())); | ||
@@ -71,8 +73,22 @@ DEBUG && log.debug('Output file: ' + output); | ||
// --------------------------------------------------------- | ||
DEBUG && log.debug('Reading template from disk at ' + template + '...'); | ||
_context.next = 6; | ||
DEBUG && log.debug(templateIsBuffer ? 'Reading template from buffer...' : 'Reading template from disk at ' + template.toString() + '...'); | ||
if (!templateIsBuffer) { | ||
_context.next = 9; | ||
break; | ||
} | ||
_context.t0 = template; | ||
_context.next = 12; | ||
break; | ||
case 9: | ||
_context.next = 11; | ||
return _fsExtra2.default.readFile(template); | ||
case 6: | ||
buffer = _context.sent; | ||
case 11: | ||
_context.t0 = _context.sent; | ||
case 12: | ||
buffer = _context.t0; | ||
newOptions = (0, _timm.set)(options, 'template', buffer); | ||
@@ -85,3 +101,3 @@ | ||
if (!(replaceImages && !options.replaceImagesBase64)) { | ||
_context.next = 26; | ||
_context.next = 32; | ||
break; | ||
@@ -95,5 +111,5 @@ } | ||
case 13: | ||
case 19: | ||
if (!(i < imgNames.length)) { | ||
_context.next = 24; | ||
_context.next = 30; | ||
break; | ||
@@ -106,6 +122,6 @@ } | ||
DEBUG && log.debug('Reading ' + imgPath + ' from disk...'); | ||
_context.next = 19; | ||
_context.next = 25; | ||
return _fsExtra2.default.readFile(imgPath); | ||
case 19: | ||
case 25: | ||
imgBuf = _context.sent; | ||
@@ -115,20 +131,20 @@ | ||
case 21: | ||
case 27: | ||
i++; | ||
_context.next = 13; | ||
_context.next = 19; | ||
break; | ||
case 24: | ||
case 30: | ||
newOptions.replaceImagesBase64 = true; | ||
newOptions.replaceImages = imgDataBase64; | ||
case 26: | ||
_context.next = 28; | ||
case 32: | ||
_context.next = 34; | ||
return (0, _mainBrowser2.default)(newOptions); | ||
case 28: | ||
case 34: | ||
report = _context.sent; | ||
if (!(_probe != null)) { | ||
_context.next = 31; | ||
_context.next = 37; | ||
break; | ||
@@ -139,3 +155,3 @@ } | ||
case 31: | ||
case 37: | ||
@@ -145,14 +161,25 @@ // --------------------------------------------------------- | ||
// --------------------------------------------------------- | ||
DEBUG && log.debug('Writing report to disk...'); | ||
_context.next = 34; | ||
shouldOutputBuffer = output === BUFFER_VALUE; | ||
DEBUG && log.debug(shouldOutputBuffer ? 'Returning buffer' : 'Writing report to disk...'); | ||
if (!shouldOutputBuffer) { | ||
_context.next = 41; | ||
break; | ||
} | ||
return _context.abrupt('return', Buffer.from(report)); | ||
case 41: | ||
_context.next = 43; | ||
return _fsExtra2.default.ensureDir(_path2.default.dirname(output)); | ||
case 34: | ||
_context.next = 36; | ||
case 43: | ||
_context.next = 45; | ||
return _fsExtra2.default.writeFile(output, report); | ||
case 36: | ||
case 45: | ||
return _context.abrupt('return', null); | ||
case 37: | ||
case 46: | ||
case 'end': | ||
@@ -159,0 +186,0 @@ return _context.stop(); |
{ | ||
"name": "docx-templates", | ||
"version": "2.7.3", | ||
"version": "2.8.0", | ||
"description": "Template-based docx report creation", | ||
@@ -5,0 +5,0 @@ "main": "lib/indexNode.js", |
@@ -58,3 +58,3 @@ # Docx-templates [![Build Status](https://travis-ci.org/guigrpa/docx-templates.svg)](https://travis-ci.org/guigrpa/docx-templates) [![Coverage Status](https://coveralls.io/repos/github/guigrpa/docx-templates/badge.svg?branch=master)](https://coveralls.io/github/guigrpa/docx-templates?branch=master) [![npm version](https://img.shields.io/npm/v/docx-templates.svg)](https://www.npmjs.com/package/docx-templates) | ||
You can also **provide a sync or Promise-returning callback function (query resolver)** instead of an object `data`: | ||
You can also **provide a sync or Promise-returning callback function (query resolver)** instead of a `data` object: | ||
@@ -71,2 +71,23 @@ ```js | ||
You can also **output to a buffer**: | ||
```js | ||
const buffer = createReport({ | ||
output: 'buffer', | ||
template: 'templates/myTemplate.docx', | ||
data: { ... }, | ||
}); | ||
``` | ||
...and **pass a buffer as an input `template`**: | ||
```js | ||
const template = // read from db, http etc as Buffer | ||
const buffer = createReport({ | ||
output: 'buffer', | ||
template, | ||
data: { ... }, | ||
}); | ||
``` | ||
Other options (with defaults): | ||
@@ -73,0 +94,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
166969
2226
453