From 675b3f5962e34714c9a1e0cc0e7cf60fef07c35b Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 17 Dec 2018 11:33:28 +0100 Subject: [PATCH] Support for roll printers (#185) * Added ability to hint papersize and cut on roleprinters * Documentation of paper formats and cuts * fixed pick printer without bounds * removed NSLog --- README.md | 3 +++ src/ios/APPPrinter.h | 2 +- src/ios/APPPrinter.m | 45 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 12b6fcc..9dfd94e 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,9 @@ The method accepts a list of attributes. Not all are supported on each platform | hidePageRange | Set to _true_ to hide the control for the page range.
Defaults to: false | Boolean | iOS | | hideNumberOfCopies | Set to _true_ to hide the control for the number of copies.
Defaults to: false | Boolean | iOS | | hidePaperFormat | Set to _true_ to hide the control for the paper format.
Defaults to: false | Boolean | iOS | +| paperWidth | Ability to hint width of the paper – iOS will get a printer supported paperformat which fits the best to this width. Only works when `paperHeight` is given. Width in millimeters. | Number | iOS | +| paperHeight | Ability to hint height of the paper – iOS will get a printer paperformat which fits the best to this heigth. Only works when `paperWidth` is given. Height in millimeters. | Number | iOS | +| paperCutLength | On roll-fed printers you can decide after how many milimeters the printer cuts the paper. | Number | iOS | | bounds | The Size and position of the print view
Defaults to: [40, 30, 0, 0] | Array | iPad | #### Further informations diff --git a/src/ios/APPPrinter.h b/src/ios/APPPrinter.h index 734ccd6..5f0128e 100755 --- a/src/ios/APPPrinter.h +++ b/src/ios/APPPrinter.h @@ -23,7 +23,7 @@ #import -@interface APPPrinter : CDVPlugin +@interface APPPrinter : CDVPlugin // this is used to cache the uiprinter making repeated prints faster @property (nonatomic) UIPrinter *previousPrinter; diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index bba5fce..fd0b340 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -76,6 +76,7 @@ self.settings = [arguments objectAtIndex:1]; UIPrintInteractionController* controller = [self printController]; + controller.delegate = self; [self adjustPrintController:controller withSettings:self.settings]; [self loadContent:content intoPrintController:controller]; @@ -98,9 +99,13 @@ NSArray* arguments = [command arguments]; NSMutableDictionary* settings = [arguments objectAtIndex:0]; - NSArray* bounds = [settings objectForKey:@"bounds"]; - CGRect rect = [self convertIntoRect:bounds]; + CGRect rect = CGRectMake(40, 30, 0, 0); //Default in documentation + if (settings != (id)[NSNull null] && [settings objectForKey:@"bounds"] != nil){ + NSArray* bounds = [settings objectForKey:@"bounds"]; + rect = [self convertIntoRect:bounds]; + } + [self presentPrinterPicker:rect]; } @@ -332,6 +337,41 @@ return controller; } +/** + * Choose paper Delegate. If Paper-Size is given it selects the best fitting papersize + */ + +- (UIPrintPaper *) printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList { + if ([[self.settings objectForKey:@"paperHeight"] doubleValue] && [[self.settings objectForKey:@"paperHeight"] doubleValue]){ + double heigth = [[self.settings objectForKey:@"paperHeight"] doubleValue]; + double width = [[self.settings objectForKey:@"paperWidth"] doubleValue]; + double dotsHeigth = 72*heigth / 25.4; //convert milimeters to dots + double dotsWidth = 72*width / 25.4; //convert milimeters to dots + CGSize pageSize = CGSizeMake(dotsHeigth, dotsWidth); + + // get best fitting paper size + UIPrintPaper* paper = [UIPrintPaper bestPaperForPageSize:pageSize withPapersFromArray:paperList]; + return paper; + } + else { + return NULL; + } +} + +/** + * cutPaper Delegate. If using roll printers like Label-Printer (brother QL-710W) you can cut paper after given length. + */ +- (CGFloat)printInteractionController:(UIPrintInteractionController *)printInteractionController + cutLengthForPaper:(UIPrintPaper *)paper { + if ([[self.settings objectForKey:@"paperCutLength"] doubleValue]){ + double cutLength = [[self.settings objectForKey:@"paperCutLength"] doubleValue]; + return 72 * cutLength / 25.4; //convert milimeters to dots + } else { + return paper.paperSize.height; + } +} + + /** * Loads the content into the print controller. * @@ -385,3 +425,4 @@ } @end + \ No newline at end of file