问题描述
我正在尝试使用浏览器中的Docx.js将Web应用导出到MS Word,但是浏览器无法识别诸如Table或Media之类的基本功能。有没有人遇到过这个问题,如果是这样,有什么解决方法?
我在控制台中收到以下错误消息: “未捕获的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>
解决方法
我在 node.js 中遇到了类似的问题。我收到了 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")],});