我正在将一个非常大的 txt 文件(~40MB)转换为 docx,但似乎该字符串对于 Node.js 来说太大而无法处理。
有什么解决办法吗?还是另一种解决方案?
我的代码:
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