Print the opened tab

Today I use this code to open a PDF file

StreamResource resourcesReport = new StreamResource(movimentacaoService.imprimirMovimentacao(novaMovimentacao.getIdMovimentacao()), "Recibo" + System.currentTimeMillis() + ".pdf");
BrowserWindowOpener opener = new BrowserWindowOpener(resourcesReport);
Button downloadButton = new Button("Imprimir Comprovante",FontAwesome.DOWNLOAD);

So when my user click in my downloadButton I open a window with my pdf file.

So its possible to when I open that windows call the famous window.print();? And its possible to put one callback in that tab? when my user close that tab I need to run some code.


I remember doing this by adding a script into the PDF itself to tell the PDF reader to open the print dialog when it’s opened. How to add the script depends on the library you are using to generate the PDF.

Another option is to create a custom client-side component that adds an
element with the PDF and calls the
function on the
. I just implemented a demo that uses the
FreemarkerLayout Add-on
using this aproach and seems to be working. Take a look at the code:

Regarding the callback on the tab, maybe you could use a similar approach using JavaScript.