BrowserFrame with pdf content in Firefox

Hi all!

First of all I want to apologize if I am about to ask something that has already been asked,
but i can’t find the answer to my question anywhere.

I have BrowserFrame in window that shows pdf content from StreamResource. When i close the window most of the times pdf content opens in a new tab (I think when the file has more pages). The same thing happens if I setVisible(false) to the window. In Chrome everything works as expected.

Can I somehow overcome the problem, so that it won’t open a new tab every time?

Here is a simple code how I add streamResource

browserFrame.setSource(createResource(fileByts, fileStr));
browserFrame.setSizeFull();
private StreamResource createResource(byte[] file, String fileName) {
  StreamResource resource = new StreamResource(() -> {
    try { return new ByteArrayInputStream(file); }
    catch (Exception e) { e.printStackTrace(); return null; } },fileName);
  resource.setMIMEType("application/pdf");
  return resource; }

Thanks,
Klemen

Hi Klemen.

I have same problem when stream is still loading.

Hi.
I didnt find a solution so I used javascript viewer for the job: http://viewerjs.org/. So now I’m not depending on the browser.

Hi,

I’m having the same problem. The problem occurs in Firefox whenever more than one or two pages are loaded. Does anyone know how to solve this issue? Thanks!

Hi,

I’ve splitted the close method in two phase for testing purpose:

@Override public void close() {
   if (browserFrame.isAttached()) {
      bodyLayout.removeComponent(browserFrame);
   else {
      super.close();
}

Obviously the window go away only after second click, but without new tab problem in Firefox.
I’ve tested a working (and dirty) single click solution:

@Override public void close() {
   if (browserFrame.isAttached()) {
      bodyLayout.removeComponent(browserFrame);
      UI.getCurrent().setPollInterval(100);
      scheduledExecutor.schedule(new WindowCloseTask(this), 350L, TimeUnit.MILLISECONDS);
   }
   else {
      super.close();
      UI.getCurrent().setPollInterval(-1);
   }
}

WindowCloseTask is a simple Runnable class

class WindowCloseTask implements Runnable{
   private Window w;
   
   WindowCloseTask(Window w){
      this.w=w;
   }

   @Override public void run() { w.close(); }
}

Thank you! That seemed to work!