[GH-ISSUE #5756] [BUG] Document Generation produces corrupt .docx when content contains LaTeX backslash sequences (e.g. \frac) #5280

Closed
opened 2026-06-05 14:53:05 -04:00 by yindo · 0 comments
Owner

Originally created by @ROlwig on GitHub (Jun 4, 2026).
Original GitHub issue: https://github.com/Mintplex-Labs/anything-llm/issues/5756

Description

When a teacher or user requests a Word document containing LaTeX math notation (e.g. $\frac{3}{5}$), the generated .docx file downloads successfully but Microsoft Word refuses to open it, reporting: "Word experienced an error trying to open the file."

Root Cause

LaTeX notation like \frac contains \f, which Node.js interprets as a form feed character (Unicode U+000C, ASCII 12). This character is passed through marked.parse() into the HTML and ultimately embedded in word/document.xml.

XML 1.0 §2.2 explicitly forbids all characters below U+0020 except:

  • U+0009 (tab)
  • U+000A (line feed)
  • U+000D (carriage return)

The form feed (U+000C) is illegal. Word's XML parser correctly rejects the file.

Other LaTeX sequences with the same problem: \b (backspace 0x08), \v (vertical tab 0x0B), and any sequence using characters in the range 0x00–0x1F.

Steps to Reproduce

  1. Enable Document Creation in Settings → Agent Skills
  2. In Teacher Assistant workspace (Agent mode), request:
    Create a 5-question math quiz about fractions in Word format
  3. The LLM generates content containing $\frac{3}{5}$, $\frac{1}{2}$ etc.
  4. Download the .docx file
  5. Open in Microsoft Word → error dialog appears

Technical Details

Confirmed by inspecting the ZIP contents of the generated .docx:

// Scanning document.xml for invalid XML characters:
Invalid chars found: 21
All are char code 12 (0x0C, form feed)
Location example: '...preserve">En la fracción $\frac{3}{5}$:...'

The file is a structurally valid ZIP with correct [Content_Types].xml, but Word's strict XML parser rejects the control character.

Environment

  • AnythingLLM v1.13.0
  • Any model that generates LaTeX in responses (Gemini 2.5 Flash confirmed)
  • Microsoft Word (any version — this is an XML spec violation)
  • Any OS

Expected Behavior

Generated .docx opens correctly in Word and displays math fractions as plain text (e.g. 3/5).

Actual Behavior

Word reports: "Word experienced an error trying to open the file. Try these suggestions: Check the file permissions... Make sure there is sufficient free memory..."

Fix

In server/utils/agents/aibitat/plugins/create-files/docx/create-docx-file.js, add content sanitization before marked.parse():

// Strip XML 1.0 invalid control characters before processing
// (e.g. \f from LaTeX \frac becomes form feed 0x0C — illegal in XML)
content = content.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');

marked.setOptions({
  gfm: true,
  breaks: true,
});

The same sanitization should likely be applied in the other create-files handlers (create-pdf-file.js, create-excel-file.js, create-pptx-presentation) for consistency.

Impact

Any user asking for document generation with mathematical content (very common for education use cases) will receive a corrupt file. This is a silent failure from the user's perspective — the file downloads successfully but cannot be opened.

Originally created by @ROlwig on GitHub (Jun 4, 2026). Original GitHub issue: https://github.com/Mintplex-Labs/anything-llm/issues/5756 ## Description When a teacher or user requests a Word document containing LaTeX math notation (e.g. `$\frac{3}{5}$`), the generated `.docx` file downloads successfully but **Microsoft Word refuses to open it**, reporting: *"Word experienced an error trying to open the file."* ## Root Cause LaTeX notation like `\frac` contains `\f`, which Node.js interprets as a **form feed character** (Unicode U+000C, ASCII 12). This character is passed through `marked.parse()` into the HTML and ultimately embedded in `word/document.xml`. **XML 1.0 §2.2** explicitly forbids all characters below U+0020 except: - U+0009 (tab) - U+000A (line feed) - U+000D (carriage return) The form feed (U+000C) is illegal. Word's XML parser correctly rejects the file. Other LaTeX sequences with the same problem: `\b` (backspace 0x08), `\v` (vertical tab 0x0B), and any sequence using characters in the range 0x00–0x1F. ## Steps to Reproduce 1. Enable Document Creation in Settings → Agent Skills 2. In Teacher Assistant workspace (Agent mode), request: `Create a 5-question math quiz about fractions in Word format` 3. The LLM generates content containing `$\frac{3}{5}$`, `$\frac{1}{2}$` etc. 4. Download the `.docx` file 5. Open in Microsoft Word → error dialog appears ## Technical Details Confirmed by inspecting the ZIP contents of the generated `.docx`: ``` // Scanning document.xml for invalid XML characters: Invalid chars found: 21 All are char code 12 (0x0C, form feed) Location example: '...preserve">En la fracción $\frac{3}{5}$:...' ``` The file is a structurally valid ZIP with correct `[Content_Types].xml`, but Word's strict XML parser rejects the control character. ## Environment - AnythingLLM v1.13.0 - Any model that generates LaTeX in responses (Gemini 2.5 Flash confirmed) - Microsoft Word (any version — this is an XML spec violation) - Any OS ## Expected Behavior Generated `.docx` opens correctly in Word and displays math fractions as plain text (e.g. `3/5`). ## Actual Behavior Word reports: *"Word experienced an error trying to open the file. Try these suggestions: Check the file permissions... Make sure there is sufficient free memory..."* ## Fix In `server/utils/agents/aibitat/plugins/create-files/docx/create-docx-file.js`, add content sanitization before `marked.parse()`: ```js // Strip XML 1.0 invalid control characters before processing // (e.g. \f from LaTeX \frac becomes form feed 0x0C — illegal in XML) content = content.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, ''); marked.setOptions({ gfm: true, breaks: true, }); ``` The same sanitization should likely be applied in the other create-files handlers (`create-pdf-file.js`, `create-excel-file.js`, `create-pptx-presentation`) for consistency. ## Impact Any user asking for document generation with mathematical content (very common for education use cases) will receive a corrupt file. This is a silent failure from the user's perspective — the file downloads successfully but cannot be opened.
yindo closed this issue 2026-06-05 14:53:05 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#5280