我正在尝试在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");
答案 0 :(得分: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;
}
答案 1 :(得分:0)
它应该这样工作。也许这可能会有所帮助:https://vaadin.com/forum/-/message_boards/view_message/1572816?
答案 2 :(得分: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);
}