How to access html file resource

I have an HTML file in themes/layouts. I am displaying it using an HtmlBox. I would also like to be able to programmatically access the html in the file and save it to a file or print it.

  • Is there anyway to get the file or the file contents from the HtmlBox?
  • How do you specify the html file in the theme as a resource and then use the getResourceAsString or getResourceAsStream functionality?
  • What would the document path be for something in themes/layouts when using the following?

        Thread.currentThread().getContextClassLoader().getResourceAsStream(documentPath);
  • How can you get the html in the file to print to the printer or save? I am using the ExportDisplay example but all that seems to do is open the html in a new tab?

            exportDisplay.show(new ByteArrayDataProvider(bytes), "test.html",
                    ExportFormat.HTML);

I am currently working around this using Vaadin getThemeResourceAsStream functionality:


    public void onSaveButtonClick() {
        UI ui = UI.getCurrent();
        InputStream htmlStream = ui.getSession().getService().getThemeResourceAsStream(ui, ui.getTheme(), "layouts/terms.html");
        if (htmlStream != null) {
            String html = inputStreamToString(htmlStream);
            byte[] bytes;
            try {
                bytes = html.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            exportDisplay.show(new ByteArrayDataProvider(bytes), "test.html",
                    ExportFormat.HTML);
        }

I’d like to figure out how to do it with Cuba Platform resources.

1 Like

Incidentally, the save button is in a dialog window opened with openWindow(“name”, WindowManager.OpenType.Dialog) in case that might make a difference.

Hi,

I recommend you use Embedded component for displaying HTML files since HtmlBoxLayout is aimed at displaying complex layouts with nested UI components.

In case of Embedded, you can store your HTML file where you want, you can even generate it on the fly:


<layout>
    <embedded id="reportEmbedded" 
              type="BROWSER" 
              width="100%" 
              height="100%"/>
</layout>

public class ExtAppMainWindow extends AbstractWindow {
    @Inject
    private Embedded reportEmbedded;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        String report = "<html><body><h1>Invoice</h1></body></html>";
        reportEmbedded.setSource("report-" + UUID.randomUUID() + ".html",
                new ByteArrayInputStream(report.getBytes(StandardCharsets.UTF_8)));
    }
}

I do not recommend to read any resources from a theme because their location can depend on deployment configuration.

If you want to read something from resources you can use Resources interface that allows you to read resources from class path and override them using conf directory.

I’ve switched to using Embedded but am still having some issues with the Resources interface. I’ve got it working when I put the html file in the code directory and specify the full path – i.e.


resources.getResourcesAsStream("/com/.../xxx.html");

However, I am having problems getting a proper resources directory to work. For example I create a “data” directory in modules/core/src which is what was done in a Mario David example (https://github.com/mariodavid/cuba-example-json-testdata). I put the html file in that directory but couldn’t seem to load it. What is the cuba.confDir setting to make this work? The example project doesn’t seem to set cuba.confDir.

Also, how can you get the html in the file to print to the printer or save? I am using the ExportDisplay example but all that seems to do is open the html in a new tab?


exportDisplay.show(new ByteArrayDataProvider(bytes), "test.html",
                ExportFormat.HTML);

If you want to use Resources to access data from a screen then you have to add the data directory to web module sources, because core is a separate application inside of Tomcat and you cannot load resources of core from web context.

Usually there is no need to change cuba.confDir property. It is set to tomcat/conf/ by default and used only for configuration overriding and hot deployment.

You can force HTML page download using ExportFormat.OCTET_STREAM:


exportDisplay.show(new ByteArrayDataProvider("<html></html>".getBytes()),
                "report.html",
                ExportFormat.OCTET_STREAM);
1 Like

Ok. Thx.