Trouble Displaying a PDF Report in an IFrame

I’m using Spring Boot Vaadin with Vaadin 23 in an application which generates a Jasper report as a PDF on the server based on our user’s entered criteria. The generated report appears and can be opened by clicking, or by setting an Anchor pointing to it. My team would like to display the report directly in a Dialog. In my view class, I’ve added the following method to display the report.

private void showReportDialog() {
    Dialog dialog = new Dialog();
    dialog.setWidth("80%");
    dialog.setHeight("80%");

    String pdfURL = "file:/" + jasperSvc.getPdfPath();
    Html iframe = new Html("<iframe src='" + pdfURL + "' width='100%' height='100%'></iframe>");
    dialog.add(iframe);

    Button closeButton = new Button("Close", event -> dialog.close());
    dialog.add(closeButton);

    dialog.open();
}    

…so I’m populating pdfURL with “file:/” concatenated with the full path to the PDF file, and this is what the user sees.


How do I successfully display the PDF file in this IFrame?

You can use “file:/” only to refer local files at clients computer, and I assume this isn’t. You should have url to resource given by Vaadin.

“url to resource given by Vaadin”? What does that look like as code?

Read the file Jasper created to temp folder to pdfBytes byte array and then use IFrame component, so that can set the resource there:

        StreamResource resource = new StreamResource("report.pdf",
                    () -> new ByteArrayInputStream(pdfBytes));

        IFrame frame = new IFrame();
        frame.getElement().setAttribute("src", resource);