gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:45 25 4
gpt4 key购买 nike

我正在尝试从 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

关于swift - 打印显示在 WKWebView 中的 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com