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,

  • Select “Override an existing view” template:
    image

  • Find report-template-detail-view.xml and select Extend layout of existing screen:
    image

  • Override the method in the ExtReportTemplateDetailView class:

    package com.company.onboarding.view.reporttemplatedetail;
    
    import com.vaadin.flow.router.Route;
    import io.jmix.flowui.view.DefaultMainViewParent;
    import io.jmix.flowui.view.ViewController;
    import io.jmix.flowui.view.ViewDescriptor;
    import io.jmix.reports.entity.ReportOutputType;
    import io.jmix.reports.entity.ReportTemplate;
    import io.jmix.reportsflowui.view.template.ReportTemplateDetailView;
    import org.apache.commons.io.FilenameUtils;
    
    import java.nio.charset.StandardCharsets;
    
    @Route(value = "reports/templates/: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.setReadOnly(
                    !secureOperations.isEntityUpdatePermitted(metadata.getClass(reportTemplate), policyStore));
    
    //        templateFileEditor.addValueChangeListener(e -> {
    //            reportTemplate.setContent(e.getValue().getBytes(StandardCharsets.UTF_8));
    //        });
    
            templateFileEditor.setMode(getCodeEditorMode(reportTemplate));
        }
    }
    

See also Extending UI.

Regards,
Konstantin

Hi Konstantin,
We have the same problem in jmix 1.5
What is the solution (workaround) for 1.5?

Hello @mile.simic1,

This issue occurs in Jmix version 2.5.1. It is related to the commit:

In version 1.5, these changes were not made.

Regards,
Nikita

Thanks Nikita,
In 1.5 and 1.6 we are facing similar problem.
When downloading the DOCX template to change something (even layout stuf), after upload of the changed version, the new generated DOCX file contains some “unreadable” content according Word.
I suppose it is due to the corrupted template.

Hi Mile,
Thank you for the clarification. We’ll check this.

Regards,
Konstantin

Hi Mile,

Problem was reproduced on version 1.7-RC. Ticket was issued: Unreadable content in DOCX report · Issue #4445 · jmix-framework/jmix
Thank you!

Regards,
Igor