In the Vaadin example of how to print a dynamically generated PDF they have a two step approach, first you click on the OkButton
and then the PrintButton
. The problem is that their code to open the PDF relies on creating a new BrowserWindowOpener
which then extends the Print
button in the ClickListener
for the OKButton
. In other words they have:
okButton.addClickListener(new ClickListener()
{
@Override
public void buttonClick(ClickEvent event)
{
// Dynamically generate PDF - this is greatly simplified.
StreamResource pdfResource = new PdfStreamResource();
BrowserWindowOpener opener = new BrowserWindowOpener(pdfResource);
opener.extend(printButton);
}
}
This is great because the Print
button is now linked to BrowserWindowOpener
and will immediately open the window/tab when you click on the PrintButton
.
The problem I have is that I don't want an OkButton
, I just want to have a PrintButton
. In this case I can't add
printButton.addClickListener(new ClickListener()
{
@Override
public void buttonClick(ClickEvent event)
{
// Dynamically generate PDF - this is greatly simplified.
StreamResource pdfResource = new PdfStreamResource();
BrowserWindowOpener opener = new BrowserWindowOpener(pdfResource);
opener.extend(printButton);
}
}
In the ClickListener
FOR THE PrintButton
because the first time you click it wont work. I then thought what if I had BrowserWindowOpener
declared outside of the ClickListener
, in the class that links the PrintButton
and ClickListener
, using something a dummy resource:
BrowserWindowOpener opener = new BrowserWindowOpener("");
opener.extend(printButton);
PrintClickListener printClickListener = new PrintClickListener(opener);
printButton.addClickListener(printClickListener);
And then pass the opener
to the ClickListener
so that I could do something like:
printButton.addClickListener(new ClickListener()
{
@Override
public void buttonClick(ClickEvent event)
{
// Dynamically generate PDF - this is greatly simplified.
StreamResource pdfResource = new PdfStreamResource();
opener.setResource(pdfResource);
}
}
However the issue here is that the first click generates a blank page.
My question is how can I get the example working where the dynamically generated PDF is pushed to the browser window with just a SINGLE BUTTON. All the examples for BrowserWindowOpener
seem to always use two buttons. And the examples that declare it higher in the class hierarchy don't work for me, it's always an initial blank page.
Lastly, I would love to be able to do all this within the ClickListener
, that is totally decoupled from everything else.