react-native-webview icon indicating copy to clipboard operation
react-native-webview copied to clipboard

[Android & iOS] Calling `window.print()` inside an HTML document has no effect

Open birdofpreyru opened this issue 2 years ago • 9 comments

The title says it all. The issue has been brought up multiple times in past (e.g. #2162, #1309, #2764), and all these issues were just closed down by the stupid "Stale issue bot", which just hides the problems under the carpet.

In my current understanding, the window.print() is just not supported by WebView for Android (see this), and it is assumed that WebView consumers should implement it on their own, by injecting their own window.print() function into a page, signalling from it to the native code that a print is needed, then using WebView's createPrintDocumentAdapter() to generated PDF presentation of the page, and then sending the outcome to printer, or saving it to the file system.

birdofpreyru avatar Feb 05 '24 01:02 birdofpreyru

PR updated to support window.print() on iOS as well. Perhaps, also works on macOS — haven't tested that yet.

birdofpreyru avatar Feb 06 '24 00:02 birdofpreyru

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

github-actions[bot] avatar Apr 07 '24 00:04 github-actions[bot]

Hi, stupid github-actions-bot, the issue sure is still there, and the only reason there is no activity on this ticket for the last 2 months is that no library maintainer bothered to merge in the PR created two months ago that fixes the issue. Thus, please don't try to sweep the issue under the carpet by closing it, as you always do.

birdofpreyru avatar Apr 07 '24 11:04 birdofpreyru

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

github-actions[bot] avatar Jun 07 '24 00:06 github-actions[bot]

No, bot, library maintainers still have not bothered to resolve the issue by merging in the PR. In the meantime, my fork of the library has this, and a few other fixes applied — just saying, for anybody struggling with the lack of solid maintenance and development in this upstream version.

birdofpreyru avatar Jun 07 '24 07:06 birdofpreyru

@birdofpreyru Hi! Maybe you also should add this window.print method not only for html but for uri (URL) as well? Just as I saw, it works only with html for now. Thanks!

Egor-Kozlov avatar Jul 11 '24 14:07 Egor-Kozlov

Hey @Egor-Kozlov, I don't get what do you mean by window.print method not only for uri (URL)? Thinking of URL API, I am not sure I know how is it related to printing?

Also, as of now, I don't have an aspiration to fix / improve all possible aspects of react-native-webview — I patched some stuff which is relevant to my personal projects, but other than that I have to focus my time on other development stuff, which actually pays me back.

birdofpreyru avatar Jul 11 '24 14:07 birdofpreyru

@birdofpreyru sure, I understand :)

Below I just described my case and my assumptions:

Just there is a library to print documents which is used for the most part of projects in web and this library is used on the website, where I need to print.

They have a sandbox, where I also tried to call "print window" in webview - but it didn't have affect. As I know, they use window.print method as well, but for some reason it doesn't work with webview (but as I texted, your solution work perfectly with html document).

Therefore, I assume that your solution works with <webview with source={{html: 'somehtml'}}/>, but not with <webview with source={{uri: 'someurl'}}/>.

Egor-Kozlov avatar Jul 11 '24 15:07 Egor-Kozlov

Therefore, I assume that your solution works with <webview with source={{html: 'somehtml'}}/>, but not with <webview with source={{uri: 'someurl'}}/>.

Agh... I see it now.

Well, my solution relies on this and this to hook to window.print() calls on Android and iOS. It might be such injections do not work when a page is loaded using uri prop; or, I guess, it might be that they only attach the .print() method to the root window in the WebView, and perhaps the page you are looking at uses some iframes that probably get their own window instances. Perhaps you may look into it, and correct these injections, and I believe it will solve the issue for you?

birdofpreyru avatar Jul 11 '24 15:07 birdofpreyru

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

github-actions[bot] avatar Sep 10 '24 00:09 github-actions[bot]