Printing PDF file that's displayed in a WKWebView
Asked Answered
H

1

7

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?

Hydroxyl answered 27/8, 2015 at 13:42 Comment(0)
R
12

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

Ringside answered 12/9, 2015 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.