Print User's Browser Window

Hi,

I am using below code to print user’s browsing window on vaadin 24. Unfortunately two instances are printed, one with user’s completed values and one before that.
Could you please let me know if I can fix duplicate printing issue?

UI.getCurrent().getPage().executeJs(“window.print();”)

Is that two separate print jobs or a single print job that contains similar layouts twice after each other?

This code is called from a java class, after a message dialog closes. I think it’s a single print job. But it shows the same page twice, one with completed values from user, and one before completing the values.

MessageDialog messageDialog = new MessageDialog(MessageDialogType.INFO_MESSAGE, “Print Screen”, “OK”,
() → UI.getCurrent().getPage().executeJs(“window.print();”));

I want to do exactly the same as on vaadin 8 Printing | Advanced Topics | Framework | Vaadin Docs. Since JavaScript no longer exists on Vaadin 24, I use window.print() but instead of one page, two are shown on print dialog, which is the same page twice, one filled with values and one before completed it. Can I do it in a different way without using window.print() ?

Does it behave the same way if you print manually from the browser’s own menu?

Without seeing your MessageDialog it is hard to know how/where you attach that last lambda where your call the JS. If you could share that and your enviroment (browser, OS) I can investigat it a bit. My wild guess is that this relates to some extra attach/detach rounds I have seen Vaadin dialog/overlay components sometimes does for reasons unknown to me.

BTW. Just by coincidence, I just recently finished one article regarding printing. It will be out probably in upcoming weeks, the demo page might help you though (source code link in top right corner).

Yes. If i click Ctrl + P to open print dialog, i get two pages of the same view. Browser is Microsoft Edge and OS windows 10 (enterprise)

I tested quickly with a Windows 11 + Edge I had an access to and there my examples at least worked as expected.

My guess is that there is something odd in your MessageDialog component, but hard to say for sure without seeing it. Is extending Dialog or is that a separate browser window that you are trying to print? The former approach/trial might cause these sympoms if it contains a “replica” is still in the dom behind the “dialog”? If you want just the dialog content to be printed, then you should use a similar iframe hack is I do in the “Printing a virtualized Grid” or “print as PDF”,