Corrupted DOCX Template Upload – Temporary Workaround, Better Solution?

Hi everyone,

I recently created a GitHub issue regarding a problem with uploading Word (DOCX) templates in Jmix Reports.

Problem Summary

After uploading a new Word template to an existing report and trying to download it again later, the resulting DOCX file appears to be corrupted and cannot be opened in Microsoft Word.

Temporary Workaround

To temporarily fix the issue, I’ve overridden the initTemplateEditor method in ReportTemplateDetailView and commented out the following code:

//        templateFileEditor.addValueChangeListener(e -> {
//            reportTemplate.setContent(e.getValue().getBytes(StandardCharsets.UTF_8));
//        });

With this change, uploading and downloading DOCX templates seems to work again.

Question

This workaround seems to solve the problem for now, but I assume it might lead to side effects elsewhere. Is there a cleaner or recommended workaround until a fix is provided in the framework?

Thanks in advance!

Edit:

Maybe that is more correct?

...
if (hasHtmlCsvTemplateOutput(outputType)) {
            String templateContent = new String(reportTemplate.getContent(), StandardCharsets.UTF_8);
            templateFileEditor.setValue(templateContent);
            templateFileEditor.addValueChangeListener(e -> {
                reportTemplate.setContent(e.getValue().getBytes(StandardCharsets.UTF_8));
            });
        }
...

Hi,
Any news about this issue ? All our reports doesn’t work in JMIX 2.5.1
Thanks,

Hi, i just override the method like this:

@Route(value = "reports/templates/ext:id", layout = DefaultMainViewParent.class)
@ViewController(id = "report_ReportTemplate.detail")
@ViewDescriptor(path = "ext-report-template-detail-view.xml")
public class ExtReportTemplateDetailView extends ReportTemplateDetailView {

  
    @Override
    protected void initTemplateEditor(ReportTemplate reportTemplate) {
        String extension = FilenameUtils.getExtension(templateUploadField.getFileName());
        if (extension == null) {
            visibleTemplateEditor(null);
            return;
        }
        ReportOutputType outputType = ReportOutputType.getTypeFromExtension(extension.toUpperCase());
        visibleTemplateEditor(outputType);
        if (hasHtmlCsvTemplateOutput(outputType)) {
            String templateContent = new String(reportTemplate.getContent(), StandardCharsets.UTF_8);
            templateFileEditor.setValue(templateContent);
            templateFileEditor.addValueChangeListener(e -> {
                reportTemplate.setContent(e.getValue().getBytes(StandardCharsets.UTF_8));
            });
        }
        templateFileEditor.setReadOnly(
                !secureOperations.isEntityUpdatePermitted(metadata.getClass(reportTemplate), policyStore));

        templateFileEditor.setMode(getCodeEditorMode(reportTemplate));
    }
}

Seems to work.

Hello @timleh,

Thank you for the detailed description of the problem. Yes, the bug was introduced in the last update. We will fix the problem in the next release.

Workaround:
You can override the method in ReportTemplateDetailView:

    protected void initTemplateEditor(ReportTemplate reportTemplate) {
        String extension = FilenameUtils.getExtension(templateUploadField.getFileName());
        if (extension == null) {
            visibleTemplateEditor(null);
            return;
        }
        ReportOutputType outputType = ReportOutputType.getTypeFromExtension(extension.toUpperCase());
        visibleTemplateEditor(outputType);
        if (hasHtmlCsvTemplateOutput(outputType)) {
            String templateContent = new String(reportTemplate.getContent(), StandardCharsets.UTF_8);
            templateFileEditor.setValue(templateContent);
        }
        templateFileEditor.setReadOnly(
                !secureOperations.isEntityUpdatePermitted(metadata.getClass(reportTemplate), policyStore));

//        templateFileEditor.addValueChangeListener(e -> {
//            reportTemplate.setContent(e.getValue().getBytes(StandardCharsets.UTF_8));
//        });

        templateFileEditor.setMode(getCodeEditorMode(reportTemplate));
    }

Regards,
Nikita Shchienko

1 Like

Hi,
Where is ReportTemplateDetailView code ?
Thanks,