1

Following the question from this post: Unable to display printer options with AirPrint

There were few questions I had:

1) One of the answers were to set Controller.printItem to a URL. Similarly, Can I set multiple URLs? Basically, I have a set of URLs I need to print at one shot. Is it possible to set controller.printitems to an array of URLs? Also, I know controller.printItem takes a type 'data', so how do I convert a web based image URL to a type 'data'?

2) For some weird reason, by default, doubled sided is set to on every time I reach the print dialog. What is the variable I need to set that off? It would be great if I could just not show the option to the user.

Community
  • 1
  • 1

2 Answers2

0

Try this code may help to you

- (IBAction)btnPrintTapped:(id)sender {
    NSData *imageData = UIImagePNGRepresentation(self.imgV.image);
    [self printItem:imageData];
}

#pragma mark - Printing 

-(void)printItem :(NSData*)data {
    printController = [UIPrintInteractionController sharedPrintController];
    if(printController && [UIPrintInteractionController canPrintData:data]) {
        printController.delegate = self;
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = [NSString stringWithFormat:@""];
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        printController.printInfo = printInfo;
        printController.showsPageRange = YES;
        printController.printingItem = data;
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if (!completed && error) {
                //NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
            }
        };
        [printController presentFromBarButtonItem:self.item animated:YES completionHandler:completionHandler];
    }
}

- (BOOL)presentFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated completionHandler:(UIPrintInteractionCompletionHandler)completion {
    return YES;
}

- (BOOL)presentFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated completionHandler:(UIPrintInteractionCompletionHandler)completion {
    return YES;
}
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • thanks for that.. I actually saw the code before posting but my situation is a little different. I have a webview in which when a user clicks on a link, I need to print 5 different pages (all pages are web URL's). So, the flow: user clicks print -> I dynamically get 5 URL's (all URL's are full page images) -> i need to send all 5 URL's to print. – tHeiMmOrTaL Apr 11 '12 at 16:43
  • i just need a simple way to send these 5 URL's to the printer and the airprint takes care of everything. But once I have the URL's, how do I convert that to NSData and once converted to NSData, how do I pass multiple URL's? – tHeiMmOrTaL Apr 11 '12 at 18:02
0

I know its late but might help if someone needs this: Create array of URLs and assign it to "printingItems" property of "UIPrintInteractionController" class.

shradha
  • 29
  • 2
  • Tried that with two PDF's, but preview does not show anything in this case. `printingItems` with one item works the same way as `printingItem` at the same time. Seems to be ios bug, or I'm missing something. Found couple more questions with the same issue. @shradha, do you know any solution to this? – ykravv May 07 '20 at 10:53