How can you print a WKWebView page on macOS?

If you build a WKWebView app you like to enable window.print() to let you print a page, or at least when a user chooses File > Print you like them to be able to print the web page they are currently viewing. How am I able to do this with WebKit on OSX?

Replies

WKWebView is limited in what you can do with it compared to Legacy WebView (also available in macOS). The only problem is that Legacy WebView is being depreciated by Apple, it also leaks memory. This leaves us with no option. If you find a way around it, please let me know as I am stuck here too.

A solution for macOS is discussed here that DOES work now (2021). It is the ONLY solution I know of that will print ALL on and offscreen content of a WKWebView. It currently is NOT working on iOS but I am working on that. See this stackoverflow post and the answer marked as correct:

https://stackoverflow.com/questions/33319295/how-does-one-print-all-wkwebview-on-and-offscreen-content-osx-and-ios

And here is the working code:

https://gist.github.com/cribaudo/7e95c26c5d7e8ffe35741cbb3158fb62