Не работает docx.js

Рейтинг: 0Ответов: 1Опубликовано: 26.04.2023

Пытаюсь разобраться с docx.js, есть код:

async function download () {
    const doc = new Document({
        sections: [
            {
                properties: {},
                children: [
                    new docx.Paragraph({
                        children: [
                            new docx.TextRun("Hello World"),
                            new docx.TextRun({
                                text: "Foo Bar",
                                bold: true,
                            }),
                            new docx.TextRun({
                                text: "\tGithub is the best",
                                bold: true,
                            }),
                        ],
                    }),
                ],
            },
        ],
    });
    const blob = await docx.Packer.toBlob(doc);
    const name = 'download.docx';
    a.href = URL.createObjectURL(blob);
    a.download = name;
    a.click();
}

document.getElementById('download').addEventListener('click', download, false)
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <button id="download" type="button">click</button>
    </body>
</html>
<script src="https://unpkg.com/docx@8.0.3/build/index.js"></script>

Почему-то выходит ошибка:

dex.js:2 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'Relationships')
at Object.xmlifyFile (index.js:2:321537)
at Object.compile (index.js:2:321269)
at Ia.<anonymous> (index.js:2:320739)
at Generator.next (<anonymous>)
at index.js:2:319772
at new Promise (<anonymous>)
at Sa (index.js:2:319517)
at Ia.toBlob (index.js:2:320682)
at HTMLButtonElement.download (js:45:36)

Как быть?

Ответы

▲ 1Принят

У меня работает вот такой вариант...

Но тут блокируется link.click().

document.getElementById('download').addEventListener('click', download, false)

function download() {
  const doc = new docx.Document({
    sections: [{
      properties: {},
      children: [
        new docx.Paragraph({
          children: [
            new docx.TextRun("Hello World"),
            new docx.TextRun({
              text: "Foo Bar",
              bold: true
            }),
            new docx.TextRun({
              text: "\tGithub is the best",
              bold: true
            })
          ]
        })
      ]
    }]
  });
  docx.Packer.toBlob(doc)
    .then((blob) => {
      //console.log(blob);
      const name = 'download.docx';
      const link = document.createElement('a');
      link.setAttribute('href', URL.createObjectURL(blob));
      link.setAttribute('download', name);
      link.click();
      console.log("Документ создан");
    })
    .catch(console.log);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/docx@8.0.3/build/index.js"></script>
</head>

<body>
  <button id="download" type="button">click</button>
</body>

</html>