打印在WKWebView中显示的PDF文件

时间:2015-08-27 13:42:37

标签: swift pdf wkwebview uiprintinteractioncntrler uiprintformatter

我正在尝试从WKWebView打印。网页和图片工作正常。

仅当我打印PDF文件时,页面都是空白的。

这是我用来创建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)

有人能帮助我走向正确的方向吗? 这个问题有解决方案吗?

1 个答案:

答案 0 :(得分:8)

根据documentation,您可以使用printingItem

重要提示:在iPhone模拟器上有点滞后,看起来需要时间将pdf直接加载到Controller。

但您需要在print方法

中设置下一个代码
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)

模拟器示例:enter image description here