Opening Pdf in new Window in PDF reader format

Hi All,

I have requirement in which i need to open a window in pdf reader format on clicking of a button. I tried using Embedded component but did not work. Please help me out with some code snippets.

Thanks

Abhilash

Have a look at this thread here:
https://vaadin.com/forum/-/message_boards/view_message/213396#_19_message_213396

Thanks Andreas for the reply. The thing is i am not going to generate PDF and xls in my application. I just need to open PDF and XLS files from vaadin in a new window using Byte Array. How can i do it? Please reply back with some code samples.

Thanks

Abhilash

  1. Create a StreamResource that returns an inputstream that accesses the byte array
  2. Use an Embedded component in a window

If you look read the comments of the
blog post
linked to, there is a fully fledged code sample.

I shall simply cut and paste it below. (I did not write, nor have I tested it.) Clearly change the file input stream to a BYteArrayInputStream.
Change the MIMEType for Excel.

Window window = new Window();
((VerticalLayout) window.getContent()).setSizeFull();
window.setResizable(true);
window.setCaption("Exemplo PDF");
window.setWidth("800");
window.setHeight("600");
window.center();
StreamSource s = new StreamResource.StreamSource() {

@Override
public InputStream getStream() {
try {
File f = new File("C:/themes/repy.pdf");
FileInputStream fis = new FileInputStream(f);
return fis;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
};

StreamResource r = new StreamResource(s, "repy.pdf", mainLayout.getApplication());
Embedded e = new Embedded();
e.setSizeFull();
e.setType(Embedded.TYPE_BROWSER);
r.setMIMEType("application/pdf");

e.setSource(r);
window.addComponent(e);
getMainWindow().addWindow(window);

Cheers,

Charles.

Hi Charles. I tried out opening the pdf and Excel file using your code. I put the pdf and excel file which i had to open in the webapp and provided the path for it. But only a empty Window will open each time. Can you please help me out to fix this issue?

Thanks
Abhilash

Let’s stick with PDF’s.

Are you absolutely sure that embedded PDF’s work in your browser? Have you got a PDF reader/plugin installed in your browser?

I copied that code directly from the post above, changed only the filename - and it worked first time.

Thanks for your suggestion Charles. My problem is solved now.

Thank you very much!!! :slight_smile:

Hi All

Even I have the same problem where I am not able to open the pdf which is in byte array. My code works fine if I use chIt showing blank window If I dnt use the chrome plugin. Can someone suggest me how can I open a pdf in new vaadin window without using any browser plugins.

Thanks