[Android & iOS] Calling `window.print()` inside an HTML document has no effect
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.
PR updated to support window.print() on iOS as well. Perhaps, also works on macOS — haven't tested that yet.
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
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.
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
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 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!
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 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'}}/>.
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?
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