0

I'm turning a very large txt file(~40MB) into docx but it seems that the string is too large for Node.js to handle.
Is there any fix for it? Or another solution?
My code:

import fs from 'fs';
import path from 'path';
import Docxtemplater from 'docxtemplater';
import Pizzip from 'pizzip';

const txt = path.parse(process.argv[2]);

const text = fs.readFileSync(process.argv[2]).toString().split('\n');

const docx = new Docxtemplater()
    .loadZip(new Pizzip(fs.readFileSync('./template.docx')))
    .setData({text: text})
    .render();
fs.writeFileSync(`./down/${txt.name}.docx`,
    docx.getZip().generate({type: 'nodebuffer'}));
<ProjectDir>\node_modules\docxtemplater\js\join-uncorrupt.js:90
    return full + part;
                ^

RangeError: Invalid string length
    at <ProjectDir>\node_modules\docxtemplater\js\join-uncorrupt.js:90:17
    at Array.reduce (<anonymous>)
    at Object.joinUncorrupt (<ProjectDir>\node_modules\docxtemplater\js\join-uncorrupt.js:51:16)
    at LoopModule.render (<ProjectDir>\node_modules\docxtemplater\js\modules\loop.js:484:24)
    at moduleRender (<ProjectDir>\node_modules\docxtemplater\js\render.js:15:30)
    at <ProjectDir>\node_modules\docxtemplater\js\render.js:45:26
    at Array.map (<anonymous>)
    at render (<ProjectDir>\node_modules\docxtemplater\js\render.js:43:24)
    at XmlTemplater.render (<ProjectDir>\node_modules\docxtemplater\js\xml-templater.js:249:22)
    at <ProjectDir>\node_modules\docxtemplater\js\docxtemplater.js:424:21
goEval
  • 1
  • 3
  • This is something that will be fixed soon in a new release of docxtemplater, probably this month or next month. – edi9999 Nov 17 '21 at 19:43

1 Answers1

0

This is something that should now be fixed in docxtemplater 3.27.1, which I've just published.

npm install docxtemplater@^3.27

should therefore fix your issue.

edi9999
  • 19,701
  • 13
  • 88
  • 127