我试图使用docx javascript library生成docx文件,但仅在客户端.
新文档的生成就像魅力一样,但我想修补一个文档使用patcher.文档仅显示了使用patchDocument(fs.readFileSync("My Document.docx")
加载服务器端的代码.没有关于客户端使用的信息.
我试过使用以下代码:
fetch('./public/template.docx')
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch template.docx');
}
return response.blob();
})
.then(blob => {
console.dir(blob);
const reader = new FileReader();
reader.onload = function(event) {
const arrayBufferData = event.target.result;
console.log(arrayBufferData);
patchDocument({
outputType: "blob",
data: arrayBufferData,
patches: {
title: {
type: PatchType.PARAGRAPH,
children: [new TextRun("NEUER TITEL")],
},
number: {
type: PatchType.PARAGRAPH,
children: [new TextRun("XX")],
}
},
});
};
reader.readAsArrayBuffer(blob);
})
.catch(error => {
console.error('Error fetching template.docx:', error);
});
我得到了以下错误:
Uncatch(in promise)错误:无法读取"加载的zip file"的数据.它是支持的JavaScript类型(String、Blob、ArrayBuffer等)吗?` 在docx.js?v = ba760d4b:16887:43
但是在使用console.log判断之后,数据实际上是一个ArrayBuffer.我也try 输入blob,结果是一样的.我对node没有经验,也不知道fs.readFileSync
会返回什么对象类型.但我猜这也是ArrayBuffer!错误还提到,它需要Blob或ArrayBuffer类型.
是我做错了什么,还是客户端不支持补丁程序?通过fs. readFileSync加载文件和从blob转换的ArrayBuffer在数据上是否存在差异?这两个不都是ArrayBuffer对象吗?