From 2fb312e34be8f4a1fbf8ca8d737db9e5ae9e60a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Wed, 22 Apr 2015 23:24:33 +0200 Subject: [PATCH] Do not present printer picker when printer is specified --- src/ios/APPPrinter.m | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index f7db1bc..f9bff4d 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -71,11 +71,19 @@ UIPrintInteractionController* controller = [self printController]; - CGRect rect = [self convertIntoRect:[settings objectForKey:@"bounds"]]; + NSString* printerId = [settings objectForKey:@"printerId"]; [self adjustPrintController:controller withSettings:settings]; [self loadContent:content intoPrintController:controller]; - [self presentPrintController:controller fromRect:rect]; + + if (printerId) { + [self sendToPrinter:controller printer:printerId]; + } + else { + CGRect rect = [self convertIntoRect:[settings objectForKey:@"bounds"]]; + + [self presentPrintController:controller fromRect:rect]; + } } /** @@ -116,7 +124,6 @@ printInfo.orientation = orientation; printInfo.jobName = [settings objectForKey:@"name"]; printInfo.duplex = [[settings objectForKey:@"duplex"] boolValue]; - printInfo.printerID = [settings objectForKey:@"printerId"]; controller.printInfo = printInfo; controller.showsPageRange = NO; @@ -128,7 +135,7 @@ * Adjusts the web view and page renderer. */ - (void) adjustWebView:(UIWebView*)page - andPrintPageRenderer:(UIPrintPageRenderer*)renderer + andPrintPageRenderer:(UIPrintPageRenderer*)renderer { UIViewPrintFormatter* formatter = [page viewPrintFormatter]; // margin not required - done in web page @@ -210,6 +217,30 @@ } } +/** + * Sends the content directly to the specified printer. + * + * @param controller + * The prepared print controller with the content + * @param printer + * The printer specified by its URL + */ +- (void) sendToPrinter:(UIPrintInteractionController*)controller + printer:(NSString*)printerId +{ + NSURL* url = [NSURL URLWithString:printerId]; + UIPrinter* printer = [UIPrinter printerWithURL:url]; + + [controller printToPrinter:printer completionHandler: + ^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) { + CDVPluginResult* pluginResult = + [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + + [self.commandDelegate sendPluginResult:pluginResult + callbackId:_callbackId]; + }]; +} + /** * Convert Array into Rect object. *