docx - Docx.js 无法识别 html javascript 中的基本功能
问题描述
我正在尝试在浏览器中使用 Docx.js 将 Web 应用程序导出到 MS Word,但浏览器无法识别表格或媒体等基本功能。有没有人遇到过这个问题,如果有,有什么解决方法?
我在控制台中收到以下错误消息: “未捕获的 ReferenceError:未定义表”
下面是我的示例代码:
<html>
<h1>DOCX browser Word document generation</h1>
<button type="button" onclick="generate()">Click to generate document</button>
<script>
function generate() {
const doc = new docx.Document();
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Hello")],
}),
new TableCell({
children: [new Paragraph("World!!!")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("bla bla bla")],
}),
new TableCell({
children: [new Paragraph("bla bla bla")],
}),
],
}),
],
});
doc.addSection({
properties: {},
children: [table],
});
docx.Packer.toBlob(doc).then(blob => {
console.log(blob);
saveAs(blob, "example.docx");
console.log("Document created successfully");
});
}
</script>
解决方案
我在节点中遇到了类似的问题。我得到了ReferenceError: HeadingLevel is not defined
。研究这个我发现这个问题在 github https://github.com/dolanmiu/docx/issues/485中打开。所以我认为你需要docx
在任何 docx 声明之前显式调用。例如。
rows: [
new docx.TableRow({
children: [
new docx.TableCell({
children: [new docx.Paragraph("Hello")],
}),
new docx.TableCell({
children: [new docx.Paragraph("World!!!")],
}),
],
}),
new docx.TableRow({
children: [
new docx.TableCell({
children: [new docx.Paragraph("bla bla bla")],
}),
new docx.TableCell({
children: [new docx.Paragraph("bla bla bla")],
}),
],
}),
],
});
推荐阅读
- python - 在 AWS Lambda 上将大 json 文件拆分为较小的 json 文件并将其保存在 S3 上
- python - 如何在不获取 PY_VAR0 作为值的情况下从 tkinter 输入框保存到文本文件?
- javascript - 如何使用与特定字符串匹配的列中的值在 d3.js 中进行可视化
- python - 如何从 influxDB API 的 JSON 响应中提取点?
- javascript - 无法读取 discord.js 中未定义的属性“解码器”
- sql-server - SQL 按具有多个唯一值的多列分组,用于分组列
- r - 解决 R 中的数值不稳定积分
- arrays - 如何用几行代码改进这个程序?
- c - 是否可以使用 fgets 将用户输入从一行存储到 2 个数组中?
- r - 在列表中的变量循环中使用自定义 ggplot 函数