可可osx打印pdf文件 [英] cocoa osx print pdf document

查看:95
本文介绍了可可osx打印pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的可可粉应用程序中,我从Webview中获取了pdf文档.我需要打印该pdf.我读到有NSPrintOperation printOperationWithView方法,但是此方法可打印文档的视图.无论如何,可以直接从文件url打印吗?我怎样才能正确地做到这一点?

In my cocoa application I obtain a pdf document from my webview. I need to print that pdf. I read that there is NSPrintOperation printOperationWithView method, but this method prints a view of the document. Is there anyway for printing directly from a file url? How can I achieve this correctly?

这是我的代码:

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];

请帮助.预先感谢.

推荐答案

阅读此答案:打印时不带NSView .

它说明了如何获取存储在NSData结构中的PDF文件并将其发送到打印机.因此,剩下的唯一挑战(除了清理绝对不是生产质量的代码)是将PDF文件放入NSData流中.那应该不是很难.

It explains how you can take a PDF file stored in an NSData structure and send it to a printer. So the only challenge left (other than cleaning up that code which it definitely not production quality) would be to get your PDF file into an NSData stream. That shouldn't be very difficult.

实际上,您可以更改部分代码,而不是使用从NSData流获取信息的数据提供程序,而是使用以下方法构造直接从文件读取的数据提供程序:

In fact, you could change part of the code and instead of using a data provider that gets information from an NSData stream, construct one that reads directly from file using:

CGDataProviderRef CGDataProviderCreateWithFilename ( const char *filename );

如果您已经在磁盘上的某个位置放置了PDF文件,那应该更加直接.

That should be even more straightforward if you already have the PDF file on disk somewhere.

这篇关于可可osx打印pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆