PavelS0 / docx_template_dart

A Docx template engine
Apache License 2.0
40 stars 20 forks source link

BUG: DocxTemplate loses reference after calling getTags #45

Closed leandromsales closed 8 months ago

leandromsales commented 8 months ago

@PavelS0

Consider the following scenario:

Step 1: I load a docx file:

(...)

DocxTemplate? template;

(...)
final data = await rootBundle.load(file.path);
final bytes = data.buffer.asUint8List();
template = await DocxTemplate.fromBytes(bytes);

Step 2: Once the template is loaded, I get its tags to show to the user:

final formFields = widget.docController.template.getTags()

Step 3: After getting values of the tags, I try to use the same reference of template and save it:

final docGenerated = await widget.docController.template.generate(content);
final of = File(filePath);
await of.writeAsBytes(docGenerated);

The problem is that the doc file is corrupted. But, if I load the DocxTemplate instance once again using fromBytes, the document is generated correctly. Alternatively, I did another test: I commented out the line that getTags() and generated the docx without reloading the DocxTemplate instance once again using fromBytes, and it worked as expected. In other words, as the title of this issue says: when I call getTags and later try to use the template reference, it generates a corrupted docx file.

Any suggestion?

Thank you!

leandromsales commented 8 months ago

Thanks to @AlexandreMaillot, he has fixed this issue, which is available here: https://github.com/AlexandreMaillot/docx_template_dart. @PavelS0, please when possible, merge his PR.