gpt4 book ai didi

objective-c - WKWebKit Mac OS NSPrintOperation

转载 作者:太空狗 更新时间:2023-10-30 03:33:21 27 4
gpt4 key购买 nike

WebKit 1 公开了 WebFrameView,我可以在其中调用打印操作。

- (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView {
NSPrintOperation *printOperation = [frameView printOperationWithPrintInfo:[NSPrintInfo sharedPrintInfo]];
[printOperation runOperation];
}

使用 WKWebKit API 我似乎无法弄清楚如何执行类似的操作或抓取哪个 View 进行打印。我所有的努力都得到了空白页。

最佳答案

ma​​cOS 11 更新:printOperation(with:)方法不再是私有(private)的,现在是我在我的应用程序中使用的解决方案。

令人惊讶的是,WKWebView 仍然不支持在 macOS 上打印,尽管遗留的 WebView 已被弃用。

查看https://github.com/WebKit/webkit/commit/0dfc67a174b79a8a401cf6f60c02150ba27334e5 ,打印在多年前作为私有(private) API 实现,但由于某种原因,一直没有公开。如果您不介意使用私有(private) API,您可以使用以下命令打印 WKWebView:

public extension WKWebView {
// standard printing doesn't work for WKWebView; see http://www.openradar.me/23649229 and https://bugs.webkit.org/show_bug.cgi?id=151276
@available(OSX, deprecated: 10.16, message: "WKWebView printing will hopefully get fixed someday – maybe in 10.16?")
private static let webViewPrintSelector = Selector(("_printOperationWithPrintInfo:")) // https://github.com/WebKit/webkit/commit/0dfc67a174b79a8a401cf6f60c02150ba27334e5


func webViewPrintOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) -> NSPrintOperation? {
guard self.responds(to: Self.webViewPrintSelector) else {
return nil
}

guard let po: NSPrintOperation = self.perform(Self.webViewPrintSelector, with: NSPrintInfo(dictionary: printSettings))?.takeUnretainedValue() as? NSPrintOperation else {
return nil
}

// without initializing the print view's frame we get the breakpoint at AppKitBreakInDebugger:
// ERROR: The NSPrintOperation view's frame was not initialized properly before knowsPageRange: returned. This will fail in a future release! (WKPrintingView)
po.view?.frame = self.bounds

return po
}
}

您可以通过添加以下内容使它成为您的 NSDocument 子类的默认打印操作:

    override func printOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) throws -> NSPrintOperation {
return myWebView.webViewPrintOperation(withSettings: printSettings) ?? try super.printOperation(withSettings: printSettings)
}

关于objective-c - WKWebKit Mac OS NSPrintOperation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27127919/

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