简体   繁体   English

如何在vaadin中打开生成的PDF?

[英]How to open a generated PDF in vaadin?

In my vaadin application I have a Table with an additional column containing a print Button . 在我的vaadin应用程序中,我有一个带有附加列的Table ,其中包含打印Button The Button calls the following util method to create a pdf and open it in a new window ( ui parameter is the button): Button调用以下util方法来创建pdf并在新窗口中打开它( ui参数是按钮):

public static void printPDF(Offer offer, AbstractComponent ui) throws IOException, DocumentException, TemplateException {
    // ... create PDF 

    FileResource resource = new FileResource(pdfFile);

    BrowserWindowOpener opener = new BrowserWindowOpener(resource);
    opener.setFeatures("");
    opener.extend(ui);
}

Now clicking the button the first time does not work. 现在,第一次单击该按钮不起作用。 Clicking it the second time works. 第二次单击它有效。 Clicking it the third time, opens two windows. 第三次单击它,将打开两个窗口。 This increases on every further click. 每次点击增加。

I also want to open the pdf using the context menu eg 我也想使用上下文菜单打开pdf

table.addActionHandler(new Handler()...

There I don't even have a button to extend. 我什至没有按钮可以扩展。 I would prefer to, not use the .extend() part and just open a new window. 我宁愿不使用.extend()部分,而只是打开一个新窗口。 How can I do that? 我怎样才能做到这一点?

EDIT : This blocks the button from opening mulitple instances, still not a nice solution and the first click does not work. 编辑 :这阻止了从打开多个实例的按钮,仍然不是一个好的解决方案,并且第一次单击不起作用。

Collection<Extension> extensions = ui.getExtensions();
for (Extension e : extensions) {
    if (e instanceof BrowserWindowOpener) {
        ((BrowserWindowOpener) e).setResource(resource);
        return;
    }
}

I guess I would need to create a BrowserWindowOpener for every print Button in my Table . 我想我会需要创建一个BrowserWindowOpener为每个打印Button在我的Table
Not a very clean solution, the table may contain lots of rows which would create a lot of BrowserWindowOpener instances which will never be used. 这不是一个很干净的解决方案,该表可能包含很多行,这些行会创建很多BrowserWindowOpener实例,这些实例将永远不会使用。 The context menu problem would not be solved as well. 上下文菜单问题也无法解决。

EDIT2: This is the other solution I tried: EDIT2:这是我尝试的另一个解决方案:

ResourceReference rr = ResourceReference.create(resource, ui, "print");
Page.getCurrent().open(rr.getURL(), "blank_");

Here I get the following error: 在这里,我得到以下错误:

Button (175) did not handle connector request for print/2016_9090_R_1634500091131558445.pdf 按钮(175)无法处理连接器对print / 2016_9090_R_1634500091131558445.pdf的请求

You can use the FileDownloader to achieve what you want. 您可以使用FileDownloader来实现所需的功能。

FileResource resource = new FileResource(pdfFile);
FileDownloader downloader = new FileDownloader(resource);
Button pdf= new Button("Download PDF");
downloader.extend(pdf);

Use this code 使用此代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM