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);
opener.extend(downloadButton);

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.
tks

Hi,

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
iframe
element with the PDF and calls the
print
function on the
iframe
. 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:
https://github.com/alejandro-du/print-pdf-demo
.

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

Cheers!