user5087998
user5087998

Reputation:

Printing PDF file that's displayed in a WKWebView

I’m trying to print from a WKWebView. Webpages and images are working fine.

Only when I print a PDF file the pages are all blank.

This is the code I've used to create a printController:

let printController = UIPrintInteractionController.sharedPrintController()

let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.General
printInfo.jobName = urlField.text!
printInfo.duplex = UIPrintInfoDuplex.LongEdge

let formatter: UIViewPrintFormatter = webView.viewPrintFormatter()
formatter.contentInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

printController.printFormatter = formatter
printController.printInfo = printInfo
printController.showsPageRange = true
printController.showsNumberOfCopies = true

printController.presentFromBarButtonItem(printButton, animated: true, completionHandler: nil)

Could someone help me into the right direction? Is there a solution for this problem?

Upvotes: 7

Views: 6986

Answers (1)

Alexey Bondarchuk
Alexey Bondarchuk

Reputation: 2022

According to documentation you can use printingItem.

IMPORTANT NOTE: It is a bit laggy on the iPhone simulator and looks like it takes time to load pdf directly to the Controller.

But you need to set next code in print method

let printController = UIPrintInteractionController.sharedPrintController()

let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.General
printInfo.jobName = (webView.URL?.absoluteString)!
printInfo.duplex = UIPrintInfoDuplex.None
printInfo.orientation = UIPrintInfoOrientation.Portrait

//New stuff
printController.printPageRenderer = nil
printController.printingItems = nil
printController.printingItem = webView.URL!
//New stuff

printController.printInfo = printInfo
printController.showsPageRange = true
printController.showsNumberOfCopies = true

printController.presentFromBarButtonItem(printButton, animated: true, completionHandler: nil)

Example from simulator:enter image description here

Upvotes: 12

Related Questions