如何使用WKWebView打印printOperation(有:)的内容
初试
let info = NSPrintInfo.shared
let operation = printOperation(with: info)
operation.run()但我收到了这样的信息:
打印错误: NSPrintOperation视图的框架在knowsPageRange:返回之前未正确初始化。(WKPrintingView)
但是,view属性在NSPrintOperation上是只读的。
二次尝试
如果我尝试以模式方式运行该操作并禁用打印面板,则如下所示:
operation.showsPrintPanel = false
guard let window = webView.window else { return }
operation.runModal(for: window, delegate: nil, didRun: nil, contextInfo: nil)我收到一个提示:
若要打印此文件,必须设置打印机。
的确,我还没有设置打印机,但是打印(即保存)到PDF又如何呢?
发布于 2021-11-04 13:20:08
我意识到你可以在NSPrintOperation上修改视图的框架。它必须是至少一个CGRect/NSRect的100x100,很明显,它调整了自己的大小,而且这个位置根本不重要,这就是我最后要做的:
let info = NSPrintInfo.shared
let operation = webView.printOperation(with: info)
operation.view?.frame = webView.bounds
guard let window = webView.window else { return }
operation.runModal(for: window, delegate: nil, didRun: nil, contextInfo: nil)https://stackoverflow.com/questions/69839911
复制相似问题