【问题标题】:Docx.js not recognizing basic functions in html javascriptDocx.js 无法识别 html javascript 中的基本功能
【发布时间】:2021-02-24 03:57:07
【问题描述】:

我正在尝试在浏览器中使用 Docx.js 将 Web 应用程序导出到 MS Word,但浏览器无法识别表格或媒体等基本功能。有没有人遇到过这个问题,如果有,有什么解决办法?

我在控制台中收到以下错误消息: “未捕获的引用错误:未定义表”

下面是我的示例代码:

<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>

【问题讨论】:

    标签: docx html-to-docx


    【解决方案1】:

    我在 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")],
                            }),
                        ],
                    }),
                ],
            });
    

    【讨论】:

    • 是的,你是对的。我实际上在一天后解决了这个问题。 Docx 的创建者实际上回应了我在 GitHub 中提交的一个问题。我只是忘了将此标记为已解决。感谢您的确认!
    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2019-08-01
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多