From 15aa295cf6ec1cff8bd8b03c098e7a90060c6899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 13 Oct 2014 23:31:12 +0200 Subject: [PATCH] Print-View positioning on iPad --- CHANGELOG.md | 1 + README.md | 10 ++++++++ src/ios/APPPrinter.m | 24 ++++++++++++++++--- www/printer.js | 56 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d70d9bf..33ad5f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Version 0.7.1 (not yet released) - [bugfix:] `isAvailable` does not block the main thread anymore. - [bugfix:] iPad+iOS8 incompatibility (Thanks to __zmagyar__) +- [enhancement:] Print-View positioning on iPad #### Version 0.7.0 (12.09.2014) - Android Printing Framework support diff --git a/README.md b/README.md index 7cf230e..96c2ad1 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ cordova plugin rm de.appplant.cordova.plugin.printer #### Version 0.7.1 (not yet released) - [bugfix:] `isAvailable` does not block the main thread anymore. - [bugfix:] iPad+iOS8 incompatibility (Thanks to __zmagyar__) +- [enhancement:] Print-View positioning on iPad #### Version 0.7.0 (12.09.2014) - Android Printing Framework support @@ -164,6 +165,7 @@ The method takes a string or a HTML DOM node. The string can contain HTML conten | duplex | Specifies the duplex mode to use for the print job.
Either double-sided (duplex:true) or single-sided (duplex:false).
Double-sided by default. | Boolean | iOS | | landscape| The orientation of the printed content, portrait or landscape.
_Portrait_ by default. | Boolean | all | | graystyle | If your application only prints black text, setting this property to _true_ can result in better performance in many cases.
_False_ by default. | Boolean | all | +| bounds | The Size & position of the print view | Array | iPad | #### Further informations - See the [isAvailable][available] method to find out if printing is available on the device. @@ -236,6 +238,14 @@ cordova.plugins.printer.print('123', { name:'Document.html', landscape:true }, f }); ``` +#### 6. Custom size and position on iPad +```javascript +// Option one +cordova.plugins.printer.print('123', { bounds:[40, 30, 0, 0] }); +// Option two +cordova.plugins.printer.print('123', { bounds:{ left:40, top:30, width:0 height: 0} }); +``` + ## Quirks diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index 6eadc31..f7db1bc 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -71,9 +71,11 @@ UIPrintInteractionController* controller = [self printController]; + CGRect rect = [self convertIntoRect:[settings objectForKey:@"bounds"]]; + [self adjustPrintController:controller withSettings:settings]; [self loadContent:content intoPrintController:controller]; - [self presentPrintController:controller]; + [self presentPrintController:controller fromRect:rect]; } /** @@ -184,10 +186,9 @@ * The prepared print controller with a content */ - (void) presentPrintController:(UIPrintInteractionController*)controller + fromRect:(CGRect)rect { if(CDV_IsIPad()) { - CGRect rect = CGRectMake(40, 30, 0, 0); - [controller presentFromRect:rect inView:self.webView animated:YES completionHandler: ^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) { CDVPluginResult* pluginResult = @@ -209,6 +210,23 @@ } } +/** + * Convert Array into Rect object. + * + * @param bounds + * The bounds + * + * @return + * A converted Rect object + */ +- (CGRect) convertIntoRect:(NSArray*)bounds +{ + return CGRectMake([[bounds objectAtIndex:0] floatValue], + [[bounds objectAtIndex:1] floatValue], + [[bounds objectAtIndex:2] floatValue], + [[bounds objectAtIndex:3] floatValue]); +} + /** * Checks either the printing service is avaible or not. * diff --git a/www/printer.js b/www/printer.js index 9af6c02..b3f6b02 100755 --- a/www/printer.js +++ b/www/printer.js @@ -26,6 +26,20 @@ var exec = require('cordova/exec'); */ exports.DEFAULT_DOC_NAME = 'unknown'; +/** + * List of all available options with their default value. + * + * @return {Object} + */ +exports.getDefaults = function () { + return { + name: exports.DEFAULT_DOC_NAME, + duplex: true, + landscape: false, + bounds: [40, 30, 0, 0] + }; +}; + /** * Checks if the printer service is avaible (iOS) * or if connected to the Internet (Android). @@ -69,12 +83,52 @@ exports.print = function (content, options, callback, scope) { if (typeof params == 'string') params = { name: params }; + params = this.mergeWithDefaults(params); + if ([null, undefined, ''].indexOf(params.name) > -1) params.name = this.DEFAULT_DOC_NAME; exec(fn, null, 'Printer', 'print', [page, params]); }; +/** + * @private + * + * Merge settings with default values. + * + * @param {Object} options + * The custom options + * + * @retrun {Object} + * Default values merged + * with custom values + */ +exports.mergeWithDefaults = function (options) { + var defaults = this.getDefaults(); + + if (options.bounds && !options.bounds.length) { + options.bounds = [ + options.bounds.left || defaults.bounds[0], + options.bounds.top || defaults.bounds[1], + options.bounds.width || defaults.bounds[2], + options.bounds.height || defaults.bounds[3], + ]; + } + + for (var key in defaults) { + if (!options.hasOwnProperty(key)) { + options[key] = defaults[key]; + continue; + } + + if (typeof options[key] != typeof defaults[key]) { + delete options[key]; + } + } + + return options; +}; + /** * @private * @@ -95,4 +149,4 @@ exports._createCallbackFn = function (callbackFn, scope) { return function () { callbackFn.apply(scope || this, arguments); }; -}; \ No newline at end of file +};