无NSView打印 [英] Printing without an NSView
问题描述
目前我正在为OSX写一个应用程序,最终需要移植到iOS。
Currently I'm writing an app for OSX which will eventually need to be ported to iOS.
需要打印的数据通过CoreGraphics绘制到一个PDF上下文中 - 工作完美。
The data that needs to be printed is being drawn via CoreGraphics into a PDF context - that is working perfectly.
我一直在阅读关于在iOS和OSX打印的Apple开发文档,讽刺的是,它实际上似乎从iOS打印将更容易。
I've been reading the Apple dev documentation on printing in both iOS and OSX, and, ironically, it actually seems printing from iOS will be easier.
在iOS , UIPrintInteractionController 的 printItem
属性可以获取包含PDF数据的NSData对象并打印。看起来应该相当简单。
On iOS, UIPrintInteractionController's printingItem
property can take an NSData object containing PDF data and print that. Looks like it should be fairly straight-forward.
另一方面,OSX(看起来像)需要使用 NSPrintOperation 类 - 但它似乎是获取数据的唯一方法一个实例是通过一个NSView。 ( + printOperationWithView:
或 + printOperationWithView:printInfo:
)。
OSX on the other hand, (looks like it) requires using the NSPrintOperation class - but it seems the only way to get data into an instance is via an NSView. (+printOperationWithView:
or +printOperationWithView:printInfo:
).
看到内容的格式化和分页已经,似乎没有意义,必须重新绘制PDF数据到像NSView的东西。
Seeing as the content is formatted and paginated already it seems rather pointless to have to re-draw the PDF data to something like an NSView.
可能有
推荐答案
这段代码并不完整,但对任何人来说,这之后,这基本上是如何直接打印从NSData流:
This code is by no means complete, but for anyone who comes across this later, this is basically how you can print directly from an NSData stream:
#define kMimeType @"application/pdf"
#define kPaperType @"A4"
- (void)printData:(NSData *)incomingPrintData {
CFArrayRef printerList; //will soon be an array of PMPrinter objects
PMServerCreatePrinterList(kPMServerLocal, &printerList);
PMPrinter myPrinter;
//iterate over printerList and determine which one you want, assign to myPrinter
PMPrintSession printSession;
PMPrintSettings printSettings;
PMCreateSession(&printSession);
PMCreatePrintSettings(&printSettings);
PMSessionDefaultPrintSettings(printSession, printSettings);
CFArrayRef paperList;
PMPrinterGetPaperList(myPrinter, &paperList);
PMPaper usingPaper;
//iterate over paperList and to set usingPaper to the paper desired
PMPageFormat pageFormat;
PMCreatePageFormatWithPMPaper(&pageFormat, usingPaper);
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)incomingPrintData);
PMPrinterPrintWithProvider(myPrinter, printSettings, pageFormat, (CFStringRef)kMimeType, dataProvider);
}
(通过核心打印参考)
谨防此代码缺少内存管理,因此您将需要使用 PMRetain()
和 PMRelease()
函数以及CoreFoundation内存管理函数。
(via Core Printing Reference)
Beware this code lacks memory management so you will need to use the PMRetain()
and PMRelease()
functions as well as the CoreFoundation memory-management functions as well.
如果任何人都能告诉我如何从OSX打印对话框获取数据,我可以在这个方法中使用我接受他们的答案,而不是这个。也就是说,不使用碳函数。
If anyone can tell me how I can get data from the OSX print dialogue into data I can use in this method I'll accept their answer instead of this. That is, without using Carbon functions.
这篇关于无NSView打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!