0

我正在尝试在 vaadin 中生成 pdf。我的问题是生成的 pdf 在当前窗口(浏览器选项卡)中打开。我试过了:

 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);

它不起作用。我错过了什么?我也试过

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

3 回答 3

3

以下为我工作(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;
}
于 2014-06-23T14:30:24.433 回答
0

我在我的项目中做了同样的工作,你可以在下面看到我的函数,它在 Vaadin 的窗口中显示“.pdf”。(忽略对象和函数的奇怪命名,因为它只是我项目的代码的和平)

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);
}
于 2013-02-12T14:13:39.110 回答
0

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

于 2013-02-11T18:12:12.580 回答