- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
WebKit 1 公开了 WebFrameView,我可以在其中调用打印操作。
- (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView {
NSPrintOperation *printOperation = [frameView printOperationWithPrintInfo:[NSPrintInfo sharedPrintInfo]];
[printOperation runOperation];
}
使用 WKWebKit API 我似乎无法弄清楚如何执行类似的操作或抓取哪个 View 进行打印。我所有的努力都得到了空白页。
最佳答案
macOS 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/
使用此代码我可以打印 pdf 文件: let dictDocuments = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.d
我有以下代码行来打印 webView 内容。 let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .use
我有一个 GitHub repository允许用户将分页 PDF 从一些 HTML 加载到 WebView 并使用 NSPrintOperation(具体来说,NSPrintSaveJob 和 sh
这个问题断断续续地困扰了我大约一年,我想也许其他人也会遇到类似的情况。 目标:在 Mac OS X 10.6-7 上,使用定义的分辨率和“高速”设置将多个 NSViews 打印到 EPSON Styl
我有一个应用程序,每当我调用 NSPrintOperation 时就会挂起。 我有一个 View ,它创建了一个单独的类(UIView),如下所示: PBPrintImage *printImage
我正在编写一个基于文档的 Mac OSX 应用程序,并且正在阅读有关使用 NSPrintOperation 的教程(默认情况下,立即在我的应用程序中按 print 会出现错误: printOpe
WebKit 1 公开了 WebFrameView,我可以在其中调用打印操作。 - (void)webView:(WebView *)sender printFrameView:(WebFrameVi
在我的应用程序开始打印之前,我必须进行一些处理,因此我在线程上执行此操作,一旦全部完成,我就会执行 dispatch_async(dispatch_get_main_queue(),^(){
在我的 Mac 应用程序中,我有一个显示一些 html 内容的 Web View 。我从该 web View 创建一个 PDFDocument,然后我想打印该文档。因此,我从文档创建一个 PDFVie
我试图找出是否可以在 Mac OS X 上打印而不显示系统打印面板,并且仍然以编程方式设置每个打印选项,甚至是第三方打印机驱动程序的打印选项。 我想使用NSPrintInfo . 这可能吗?是否所有
我正在尝试将 HTML 转换为 OS X 上的 PDF 文件。经过大量挖掘,我发现最好的方法是使用 NSPrintOperation。这是我用来将 HTML 字符串转换为 PDF 的代码: WebVi
我是一名优秀的程序员,十分优秀!