How does one Print all WKWebView On AND Offscreen content OSX and iOS

前端 未结 4 1040
予麋鹿
予麋鹿 2020-12-08 10:13

This question is about printing ALL content (including off screen content) of WKWebView. Currently (still, as of iOS 10.2 or OSX 10.12) there is NO working solution

4条回答
  •  不知归路
    2020-12-08 10:42

    This isn't the correct answer because Apple needs to supply the correct answer with a working print method or .evaluateJavaScript("window.print()", completionHandler: nil)

    But I have a stupid solution that "works" for me and perhaps it will help other people with a work around until then.

    Step 1: Grab the HTML and fixup the tag with . If you are fetching the html from someplace and not loading your own, you'll want to use some regular expressions. I won't go into that.

    Step 2: Save the html in a file someplace and keep the full path in a variable. In my example: filename

    Step 3: Wire your print button to this code:

    RunCommand(command: "/usr/bin/open \(filename)")
    

    See code for RunCommand below. This leaves a dumb safari window laying around, but it makes it possible to get something printed without saving to a file and then remembering where you stuck it so you can open it with Safari on your own to do the printing.

    func RunCommand(command: String)  -> (success: Bool, result: String) {
            let cc = command.components(separatedBy: " ")
            let process = Process()
            process.launchPath = cc[0]
            var cp: [String] = []
            for i in (1.. 0) {
                let output = String(data: data, encoding: String.Encoding.utf8)
                // let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
                return (success: true, result: output!)
            }
            return (success: true, result: "")
    
        }
    

    It would be really nice if Apple could fix this. I have an in-house app that does all its reporting in HTML and reports are something you kind of want to print.

提交回复
热议问题