Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display PDF in Vaadin version 14+

Tags:

vaadin14

What is the best way to display PDF file in Vaadin 14? i want to display a pdf file in a dialog, but i'm not sure how to render the pdf files. I saw some post about embedded pdf view,pdf browser and EmbeddedPdfDocument, but i can't tell if they are compatible with 14 or not.Is there a new method to do this?

like image 558
John Avatar asked Sep 06 '25 01:09

John


1 Answers

There is a third party addon to render a PDF in Vaadin 14. Your can find it here: https://vaadin.com/directory/component/pdf-browser/

That gives you the possibility to render a pdf with this code:

StreamResource streamResource = new StreamResource(
        "report.pdf", () -> getClass().getResourceAsStream("/report.pdf")); // file in src/main/resources/

PdfBrowserViewer viewer = new PdfBrowserViewer(streamResource);
viewer.setHeight("100%");
layout.add(viewer);

Alternatively you can do it in same way as it was commonly done in previous Vaadin framework versions, embedding in IFrame (see Show PDF in a Vaadin View ), which could look something like this

StreamResource streamResource = new StreamResource(
                getPresenter().createPdfStreamSource(), report.getName() + ".pdf");
StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
        IFrame iframe = new IFrame(registration.getResourceUri().toString());
        iframe.setHEight("100%");
        layout.add(iframe);
like image 163
Jean-Christophe Gueriaud Avatar answered Sep 08 '25 00:09

Jean-Christophe Gueriaud