Saving WebView to PDF returns blank image?

cocoa, macos, swift, wkwebview

Solution

iOS 11.0 and above, Apple has provided following API to capture snapshot of WKWebView.

@available(iOS 11.0, *)
    open func takeSnapshot(with snapshotConfiguration: WKSnapshotConfiguration?, completionHandler: @escaping (UIImage?, Error?) -> Swift.Void)

Sample usage:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        if #available(iOS 11.0, *) {
            webView.takeSnapshot(with: nil) { (image, error) in
                //Do your stuff with image
            }
        }
    }

iOS 10 and below, UIWebView has to be used to capture snapshot. Following method can be used to achieve that.

func webViewDidFinishLoad(_ webView: UIWebView) {

        let image = captureScreen(webView: webView)
        //Do your stuff with image
    }

func captureScreen(webView: UIWebView) -> UIImage {
        UIGraphicsBeginImageContext(webView.bounds.size)
        webView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }

Here's another relevant answer

Problem

I'm trying to figure out how to save a WebView to a PDF and totally stuck, would really appreciate some help? I'm doing this in Cocoa & Swift on OSX, here's my code so far: ``` import Cocoa import WebKit class ViewController: NSViewController { override func loadView() { super.loadView() } override func viewDidLoad() { super.viewDidLoad() loadHTMLString() } func loadHTMLString() { let webView = WKWebView(frame: self.view.frame) webView.loadHTMLString("<html><body><p>Hello, World!</p></body></html>", baseURL: nil) self.view.addSubview(webView) createPDFFromView(webView, saveToDocumentWithFileName: "test.pdf") } func createPDFFromView(view: NSView, saveToDocumentWithFileName fileName: String) { let pdfData = view.dataWithPDFInsideRect(view.bounds) if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first { let documentsFileName = documentDirectories + "/" + fileName debugPrint(documentsFileName) pdfData.writeToFile(documentsFileName, atomically: false) } } } ``` It's pretty simple, what I'm doing is creating a WebView and writing some basic html content to it which renders this: And then takes the view and saves it to a PDF file but that comes out blank: I've tried grabbing the contents from the webView and View but no joy. I've found a similar problem here How to take a screenshot when a webview finished rending regarding saving the webview to an image, but so far no luck with an OSX Solution. Could it be something to do with the document dimensions? or that the contents is in a subview? maybe if you capture the View you can't capture the SubView? Any ideas?

Original source