dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.24k stars 479 forks source link

"Word experienced an error" from missing xmlns #2697

Open joshkel opened 3 months ago

joshkel commented 3 months ago

We're using the patcher feature to generate Word documents from user-submitted templates. One of our templates has a very minimal <w:document> header within its word/document.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

When we open the resulting Word document, Word reports an error:

Word experienced an error trying to open the file. Try these suggestions.

  • Check the file permissions for the document or drive.
  • Make sure there is sufficient free memory and disk space.
  • Open the file with the Text Recovery converter.

xmllint seems to think that the problem is caused by missing xmlns namespaces:

word/document.xml:1: namespace error : Namespace prefix wp on inline is not defined
:r></w:p><w:p><w:r><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0"
                                                                               ^
word/document.xml:1: namespace error : Namespace prefix wp on extent is not defined
ine distT="0" distB="0" distL="0" distR="0"><wp:extent cx="6400800" cy="4800600"
                                                                               ^
word/document.xml:1: namespace error : Namespace prefix wp on effectExtent is not defined
"><wp:extent cx="6400800" cy="4800600"/><wp:effectExtent t="0" r="0" b="0" l="0"
                                                                               ^

If I manually add the missing namespaces, Word seems happy.