打印显示在 WKWebView 中的 PDF 文件
Printing PDF file that's displayed in a WKWebView
我正在尝试从 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 直接加载到控制器需要时间。
但是你需要在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)
来自模拟器的示例:
我正在尝试从 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 直接加载到控制器需要时间。
但是你需要在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)
来自模拟器的示例: