How to print a NSString using AirPrint?
Asked Answered
R

1

5

Im having real trouble with this one. I dont know what is wrong, but it spits this out:

ERROR: attempting to display print options with no printing source(item/items/formatter/renderer) set

The code works if i set it to print a png image, but not the contents of a NSString.

Here is my code:

- (IBAction)pushPrint:(id)sender {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
    [dateFormatter setDateFormat:@"dd/MM/YY     HH:mm:ss"];
    NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release]; 

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

    NSString *path = [[NSBundle mainBundle] pathForResource:t[NSString stringWithFormat:@"Results Printout\n"
                                                              "\n"
                                                              "%@\n"
                                                              "\n"
                                                              "\n"
                                                              "%@", currentTime, pasteboard.string] ofType:nil];

    NSData *myData = [NSData dataWithContentsOfFile:path];
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

    print.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = [path lastPathComponent];
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print.printInfo = printInfo;
    print.showsPageRange = YES;
    print.printingItem = myData;

    void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"houston we have a problem");
        }
    };
    [print presentAnimated:YES completionHandler:completionHandler];
}
Rabkin answered 21/2, 2011 at 16:14 Comment(0)
D
8

You seem to be wanting to print a plain NSString, yet you are trying to load a file with a file path that cannot be valid. Thus, myData is very likely nil (please confirm with the debugger) and thus the error message is correct (print.printingItem must not be nil).

To print a simple text, create an instance of UISimpleTextPrintFormatter (with initWithText:) and assign that instance to print.printFormatter.

Durative answered 21/2, 2011 at 17:59 Comment(2)
I'm trying to print the content of a uitextview, but the printer simulator prints it all in one line. If I replace "\n" with "\n\n", the result isn't always the same on the printer simulator. I use a UISimpleTextPrintFormatter and initWithText. Did you have similar problems?Gemmell
@Ole Begemann Thanks Man! You have helped me many times! :-) I want to know how can I come to know that something is missing in my code.Targum

© 2022 - 2024 — McMap. All rights reserved.