在我的 cocoa 应用程序中,我从我的 WebView 中获取了一个 pdf 文档。我需要打印那个pdf。我读到有 NSPrintOperation printOperationWithView 方法,但该方法打印文档的 View 。有没有直接从文件网址打印的方法?我怎样才能正确地实现这一点?
这是我的代码:
NSString *fileName = [NSString stringWithFormat:@"%@/mypdf.pdf", documentsDirectory];
NSData *pdfFinal = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];
PDFDocument *doc = [[PDFDocument alloc] initWithData:pdfFinal];
[doc writeToFile:fileName];
需要帮助请。提前致谢。
最佳答案
阅读此答案:Printing without an NSView .
它解释了如何获取存储在 NSData 结构中的 PDF 文件并将其发送到打印机。因此,剩下的唯一挑战(除了清理代码,这绝对不是生产质量)是将您的 PDF 文件放入 NSData 流中。这应该不是很困难。
事实上,您可以更改部分代码,而不是使用从 NSData 流获取信息的数据提供程序,而是使用以下方法构建直接从文件读取的数据提供程序:
CGDataProviderRef CGDataProviderCreateWithFilename ( const char *filename );
如果您的磁盘上已有 PDF 文件,那么这应该更加简单。
关于macos - cocoa OSX 打印 pdf 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29750605/