macos - cocoa OSX 打印 pdf 文档

标签 macos cocoa pdf printing

在我的 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/

相关文章:

iphone - 同级 View 之间的 NSNotification

objective-c - 注册一个在程序退出前调用的函数

macos - 更改在 OS X 中使用 popover segue 创建的 popover 的外观

macos - 无法使用 ssh-add 加载私钥

macos - Homebrew 安装问题 : in `<main>' : undefined local variable or method `“' for main:Object (NameError)

pdf - 为什么在 PHP 中使用 FPDF 亚洲 unicode 字符没有出现在 PDF 上?

java - 在java中的新窗口中下载PDF

java - 为什么我不能用 iText 垂直打印这个字符串?

linux - 在 OS X 上替换 chmod --reference?

iphone - 开始对 OS X 进行逆向工程?