簡體   English   中英

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

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

在我的vaadin應用程序中,我有一個帶有附加列的Table ,其中包含打印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);
}

現在,第一次單擊該按鈕不起作用。 第二次單擊它有效。 第三次單擊它,將打開兩個窗口。 每次點擊增加。

我也想使用上下文菜單打開pdf

table.addActionHandler(new Handler()...

我什至沒有按鈕可以擴展。 我寧願不使用.extend()部分,而只是打開一個新窗口。 我怎樣才能做到這一點?

編輯 :這阻止了從打開多個實例的按鈕,仍然不是一個好的解決方案,並且第一次單擊不起作用。

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

我想我會需要創建一個BrowserWindowOpener為每個打印Button在我的Table
這不是一個很干凈的解決方案,該表可能包含很多行,這些行會創建很多BrowserWindowOpener實例,這些實例將永遠不會使用。 上下文菜單問題也無法解決。

EDIT2:這是我嘗試的另一個解決方案:

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

在這里,我得到以下錯誤:

按鈕(175)無法處理連接器對print / 2016_9090_R_1634500091131558445.pdf的請求

您可以使用FileDownloader來實現所需的功能。

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

使用此代碼

    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