开发者

Printing without an NSView

开发者 https://www.devze.com 2023-02-08 10:13 出处:网络
Currently I\'m开发者_运维百科 writing an app for OSX which will eventually need to be ported to iOS.

Currently I'm开发者_运维百科 writing an app for OSX which will eventually need to be ported to iOS.

The data that needs to be printed is being drawn via CoreGraphics into a PDF context - that is working perfectly.

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.

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 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:).

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.

Could there possibly be another way of achieving this that I've missed?


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);
}

(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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消