calin
calin

Reputation: 163

How to open resource in new window in vaadin?

I'm trying to generate a pdf in vaadin. My problem is that the generated pdf opens in the current window(browser tab). I tried:

 String filename = contentDataName + ".pdf";
    StreamResource resource = new StreamResource(source, filename, vaadinApplication);
    resource.getStream().setContentType("application/pdf");
    resource.getStream().setFileName(filename);
    resource.getStream().setParameter("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    resource.getStream().setParameter("Content-Length", Integer.toString(fopOutput.size()));
    resource.setCacheTime(5000);
    resource.setMIMEType("application/pdf");

    mainWindow.open(resource);
    mainWindow.open(resource, "_blank", true);

It doesn't work. What am i missing? I also tried

mainWindow.open(resource, "_blank");

Upvotes: 0

Views: 12457

Answers (3)

Amit S
Amit S

Reputation: 161

Following works for me (Vaadin 7)

    ClickListener getHelpButtonListener(final Button helpButton)
{
log.debug("+++++++++ setButtonListener ...++++++++++++++===.........");
final ClickListener helpButtonlistener = new ClickListener()
{
    @Override
    public void buttonClick(ClickEvent event)
    {

    StreamSource source = new StreamSource()
    {
        public java.io.InputStream getStream()
        {
        try
        {
            String helpFilePath = basepath + "/datafiles/";
            File helpFile = new File(helpFilePath);
            FileInputStream helpFileInputStream = new FileInputStream(helpFile);
            return helpFileInputStream;
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
        }
    };
    String filename = basepath + "/datafiles/help.pdf";
    StreamResource resource = new StreamResource(source, filename);
    resource.setMIMEType("application/pdf");
    resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + filename);
    BrowserWindowOpener opener = new BrowserWindowOpener(resource);
    opener.extend(helpButton);
    }

};
return helpButtonlistener;
}

Upvotes: 3

bitguider
bitguider

Reputation: 575

i did same job in my project, you can see my function below which is showing ".pdf" inside of Vaadin's Window. (ignore strange naming of objects and functions because its just peace of code of my project)

private void publishReport(final String path){
    StreamSource s = new StreamSource() {

        public java.io.InputStream getStream() {
            try {
                File f = new File(path);
                FileInputStream fis = new FileInputStream(f);
                return fis;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    };

    StreamSource ss = new StreamSource(){


        @Override
        public InputStream getStream() {
            // TODO Auto-generated method stub
            return null;
        }

    };

    StreamResource r = new StreamResource(s, "appointmentScheduleDate.pdf", app);
    r.setCacheTime(-1);
    Embedded e = new Embedded();
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);

    r.setMIMEType("application/pdf");

    e.setSource(r);
    e.setHeight("650px");
    plaintReoprtWindow = new Window("Report");
    plaintReoprtWindow.center();
    plaintReoprtWindow.setModal(true);
    plaintReoprtWindow.setHeight("700px");
    plaintReoprtWindow.setWidth("900px");
    plaintReoprtWindow.addComponent(e);
    app.getMainWindow().addWindow(plaintReoprtWindow);
}

Upvotes: 0

Jan Galinski
Jan Galinski

Reputation: 11993

It should work that way. Maybe this might be helpful: https://vaadin.com/forum/-/message_boards/view_message/1572816?

Upvotes: 0

Related Questions