guigrpa / docx-templates

Template-based docx report creation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Comments in template in FOR cause 'unreadable content' when opening created Word doc

rbuhrs opened this issue · comments

@jjhbw as discussed

When a template contains notes in a FOR loop the creation of the document succeeds, but when you open the document Word shows a 'unreadable content' error.

image

A created a test for this here https://github.com/rbuhrs/docx-templates/blob/acf973a22cf9e4ed20d6ac8d2d4aec63bf981aef/src/__tests__/templating.test.ts#L133

It creates a document in the test directory, which shows the error when you open it in Word

Great find! I agree that this is a bug.

I checked in the XML of the corrupt .docx file that your test generates, and apparently each comment is connected to a piece of text in document.xml using a unique ID. The comments themselves are stored in separate xml files. I think Word does not like it when a comment ID refers to multiple pieces of text in the document, which is the case after our FOR loop creates copies of the text snippets.

No easy fix comes to mind, though.

Clear, thanks for looking into this!

It probably won't need a fix, as I can't think of a use case for comments in a FOR loop, but good to know this can lead to issues.

An easy workaround is available, we can just delete the comment in the FOR, or move it somewhere else out of the loop. That will solve the issue.