1
votes

I was using normal WebView component in my old Cocoa project and it was possible to override

-(void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView

to show print dialog when it's necessary.

How can I do the same with WKWebView? I couldn't find any methods about printing in the documentation.

1

1 Answers

1
votes

I have the following method to AirPrint (e.g. show print dialog) on a WKWebView, using Swift. It will pop up the print dialog, and will format text correctly across multiple pages if needed. I call printPage from my "Print" button on the app.

webViewX is a WKWebView, defined elsewhere in the class:

var webViewX: WKWebView!
//do all the work for populating the webViewX somewhere else in the class

@objc func printPage(sender: AnyObject)
{
    let pInfo :UIPrintInfo =  UIPrintInfo.printInfo()
    pInfo.outputType = UIPrintInfo.OutputType.general
    pInfo.jobName = self.title //page title defined elsewhere
    pInfo.orientation = UIPrintInfo.Orientation.portrait

    let printController = UIPrintInteractionController.shared
    printController.printInfo = pInfo
    printController.printFormatter = webViewX.viewPrintFormatter()
    printController.present(animated: true, completionHandler: nil)
}