gpt4 book ai didi

swift Mac 操作系统 : blank page printed when I try to print webView (WKWebView)

转载 作者:行者123 更新时间:2023-12-05 07:39:52 26 4
gpt4 key购买 nike

我使用以下代码让 webView 呈现给定的 HTML 字符串:

let filePath = Bundle.main.path(forResource: "my file name", ofType: "html")
let htmlStr = try! NSString.init(contentsOfFile: filePath!, encoding: String.Encoding.utf8.rawValue)
webView.loadHTMLString(htmlStr as String, baseURL: nil)

webView 正确显示 HTML,但是当我尝试打印它时,打印面板出现空白页,我使用了这个:

webView.printView(nil)

我也试过这个:

NSPrintOperation(view: webView).run()

所有出现的打印面板都是空白页!有什么帮助吗?

最佳答案

run() 不适用于 OSX 上的 WKWebView。使用 runModal(for:delegate:didRun:contextInfo) 代替。示例:

let printInfo = NSPrintInfo.shared
printInfo.horizontalPagination = .fit
printInfo.verticalPagination = .automatic
printInfo.isVerticallyCentered = true
printInfo.isHorizontallyCentered = true
printInfo.orientation = .portrait
printInfo.topMargin = 20
printInfo.bottomMargin = 20
printInfo.rightMargin = 20
printInfo.leftMargin = 20

let op = self.webView.printOperation(with: printInfo)
op.showsPrintPanel = true
op.showsProgressPanel = true
op.view?.frame = self.webView.bounds
op.runModal(for: self.view.window!, delegate: self, didRun: nil, contextInfo: nil)

我从:How does one Print all WKWebView On AND Offscreen content OSX and iOS 得到这个

关于 swift Mac 操作系统 : blank page printed when I try to print webView (WKWebView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46777468/

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