Show pdf from filesystem outside project directory

Hi,

I try to show a pdf inside Vaadin iframe.
The app is deployed as a war file into Tomcat 10.
All the files are located outside the Tomcat folder structure.

After hours of research…
how to show a pdf inside an iFrame in a view?

Using JMIX 2.0.3 with newest plugin and IDEA version, openJDK 17…

BR
Roland

I got a little bit further (but maybe in the wrong direction?)

I added a @Configuration and in there a resourceHandler
In the view I added an iFrame inside a formLayout…
And then I tried this coding, but I get an error: stream closed
when I try to set the iframe source.

Any tip would be great.

public void onShowPDFClick(final ClickEvent<JmixButton> event) {
    String fileName = "0aatest.pdf";
    String fileNamePath = "objectplan/0aatest.pdf";

    try {
        File file = ResourceUtils.getFile(fileNamePath);
        System.out.println(file.exists());
        StreamResource resource = new StreamResource(fileName, () -> new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(fileNamePath)));
        StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
        System.out.println(registration.getResourceUri().toString());
        iframeID.setSrc(registration.getResourceUri().toString());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

the system.out:
true
VAADIN/dynamic/resource/1/e9e6882d-70ba-40f4-bcb9-1e304ee0c95d/0aatest.pdf
2023-09-10T22:24:16.252+02:00 ERROR 47960 — [nio-8080-exec-9] i.j.f.exception.UiExceptionHandlers : There is no io.jmix.flowui.exception.UiExceptionHandler can handle the exception

java.io.IOException: Stream closed
at java.base/java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:157) ~[na:na]
at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:244) ~[na:na]
at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:284) ~[na:na]
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:343) ~[na:na]…

I use an image now… that works… can show the pdf and can resize it like needed.

public void onShowPDFClick(final ClickEvent event) {
String fileName = “0aatest.pdf”;
String fileNamePath = “objectplan/0aatest.pdf”;

    try {
        Path pdfPath = Paths.get(fileNamePath);
        byte[] pdf = Files.readAllBytes(pdfPath);
        ByteArrayInputStream in = new ByteArrayInputStream(pdf);
        StreamResource resource2 = new StreamResource(fileName, () -> in);
        imageID.setSrc(resource2);
        imageID.setWidth(defaultSize, Unit.PIXELS);
        imageID.setHeight(defaultSize, Unit.PIXELS);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
1 Like