当前位置 : 主页 > 手机开发 > 其它 >

swift – 打印在WKWebView中显示的PDF文件

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在尝试从WKWebView打印.网页和图片工作正常. 仅当我打印PDF文件时,页面都是空白的. 这是我用来创建printController的代码: let printController = UIPrintInteractionController.sharedPrintController()let
我正在尝试从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)

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

根据 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

网友评论