Skip to content
This repository

Additional functionality for the PrintPlugin #290

Closed
wants to merge 1 commit into from

2 participants

Tim Robertson @RandyMcMillan
Tim Robertson

Update the plugin to use the newer PhoneGap.exec(callbackSuccessFunction,callbackErrorFunction, PLUGINNAME, PLUGINMETHODNAME, paramObj); implementation.

The plugin now has additional control over the print job. It is now possible to set any of the printInfo settings. Updated the README with info about fixing EXC_BAD_ACCESS in the iOS5 Simulator (copied from the Twitter plugin README).

Tim Robertson funkjedi updated PrintPlugin for the current PhoneGap.exec
added additional configuration options to the print method the plugin
now has as much control as possible over the print job from javascript
edc83a0
@RandyMcMillan

We can take a look at this after a rewrite to support Cordova. Sorry for the delayed response. Check out the "Cordova Plugin Upgrade Guide.pdf" currently bundled with the Cordova 1.5 download. http://phonegap.com/download-thankyou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 19, 2011
Tim Robertson funkjedi updated PrintPlugin for the current PhoneGap.exec
added additional configuration options to the print method the plugin
now has as much control as possible over the print job from javascript
edc83a0
This page is out of date. Refresh to see the latest.
18 iPhone/PrintPlugin/PrintPlugin.h
@@ -8,7 +8,6 @@
8 8 //
9 9
10 10 #import <Foundation/Foundation.h>
11   -
12 11 #ifdef PHONEGAP_FRAMEWORK
13 12 #import <PhoneGap/PGPlugin.h>
14 13 #else
@@ -17,27 +16,12 @@
17 16
18 17
19 18 @interface PrintPlugin : PGPlugin {
20   - NSString* successCallback;
21   - NSString* failCallback;
22   - NSString* printHTML;
23   -
24   - //Options
25   - NSInteger dialogLeftPos;
26   - NSInteger dialogTopPos;
27 19 }
28 20
29   -@property (nonatomic, copy) NSString* successCallback;
30   -@property (nonatomic, copy) NSString* failCallback;
31   -@property (nonatomic, copy) NSString* printHTML;
32   -
33   -//Print Settings
34   -@property NSInteger dialogLeftPos;
35   -@property NSInteger dialogTopPos;
36 21
37   -//Print HTML
38 22 - (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
39 23
40   -//Find out whether printing is supported on this platform.
41 24 - (void) isPrintingAvailable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
42 25
  26 +
43 27 @end
173 iPhone/PrintPlugin/PrintPlugin.js
@@ -5,81 +5,114 @@
5 5 */
6 6
7 7 var PrintPlugin = function() {
  8 +};
8 9
9   -}
10 10
11   -PrintPlugin.prototype.callbackMap = {};
12   -PrintPlugin.prototype.callbackIdx = 0;
  11 +PrintPlugin.UIPrintInfoOutputType = {
  12 + UIPrintInfoOutputGeneral: 0, // B&W or color, normal quality output for mixed text, graphics, and images
  13 + UIPrintInfoOutputPhoto: 1, // B&W or color, best quality output for images
  14 + UIPrintInfoOutputGrayscale: 2, // B&W, fast/draft quality output
  15 +};
13 16
14   -/*
15   - print - html string or DOM node (if latter, innerHTML is used to get the contents). REQUIRED.
16   - success - callback function called if print successful. {success: true}
17   - fail - callback function called if print unsuccessful. If print fails, {error: reason}. If printing not available: {available: false}
18   - options - {dialogOffset:{left: 0, right: 0}}. Position of popup dialog (iPad only).
19   - */
20   -PrintPlugin.prototype.print = function(printHTML, success, fail, options) {
21   - if (typeof printHTML != 'string'){
22   - console.log("Print function requires an HTML string. Not an object");
23   - return;
24   - }
25   -
26   -
27   - //var printHTML = "";
28   -
29   - var dialogLeftPos = 0;
30   - var dialogTopPos = 0;
31   -
32   -
33   - if (options){
34   - if (options.dialogOffset){
35   - if (options.dialogOffset.left){
36   - dialogLeftPos = options.dialogOffset.left;
37   - if (isNaN(dialogLeftPos)){
38   - dialogLeftPos = 0;
39   - }
40   - }
41   - if (options.dialogOffset.top){
42   - dialogTopPos = options.dialogOffset.top;
43   - if (isNaN(dialogTopPos)){
44   - dialogTopPos = 0;
45   - }
46   - }
47   - }
48   - }
49   -
50   - var key = 'print' + this.callbackIdx++;
51   - window.plugins.printPlugin.callbackMap[key] = {
52   - success: function(result) {
53   - delete window.plugins.printPlugin.callbackMap[key];
54   - success(result);
55   - },
56   - fail: function(result) {
57   - delete window.plugins.printPlugin.callbackMap[key];
58   - fail(result);
59   - },
60   - };
61   -
62   - var callbackPrefix = 'window.plugins.printPlugin.callbackMap.' + key;
63   - return PhoneGap.exec("PrintPlugin.print", printHTML, callbackPrefix + '.success', callbackPrefix + '.fail', dialogLeftPos, dialogTopPos);
  17 +PrintPlugin.UIPrintInfoOrientation = {
  18 + UIPrintInfoOrientationPortrait: 0,
  19 + UIPrintInfoOrientationLandscape: 1,
64 20 };
65 21
66   -/*
67   - * Callback function returns {available: true/false}
68   - */
69   -PrintPlugin.prototype.isPrintingAvailable = function(callback) {
70   - var key = 'isPrintingAvailable' + this.callbackIdx++;
71   - window.plugins.printPlugin.callbackMap[key] = function(result) {
72   - delete window.plugins.printPlugin.callbackMap[key];
73   - callback(result);
74   - };
75   -
76   - var callbackName = 'window.plugins.printPlugin.callbackMap.' + key;
77   - PhoneGap.exec("PrintPlugin.isPrintingAvailable", callbackName);
  22 +PrintPlugin.UIPrintInfoDuplex = {
  23 + UIPrintInfoDuplexNone: 0,
  24 + UIPrintInfoDuplexLongEdge: 1, // flip back page along long edge (same orientation in portrait, flipped for landscape)
  25 + UIPrintInfoDuplexShortEdge: 2, // flip back page along short edge (flipped orientation for portrait, same in landscape)
78 26 };
79 27
  28 +
  29 +PrintPlugin.prototype.print = function(settings) {
  30 + settings = PrintPlugin.defaults(settings, {
  31 + html: '',
  32 + outputType: 'UIPrintInfoOutputGeneral',
  33 + jobName: '',
  34 + duplex: 'UIPrintInfoDuplexLongEdge',
  35 + orientation: 'UIPrintInfoOrientationPortrait',
  36 + showsPageRange: true,
  37 + presentFromRect: [0,0,0,0],
  38 + success: function(result) {},
  39 + failure: function(result) {}
  40 + });
  41 +
  42 + // process the presentFromRect and convert to string value understood by CGRectFromString
  43 + if (Object.prototype.toString.apply(settings.presentFromRect) === '[object Array]' && settings.presentFromRect.length === 4) {
  44 + settings.presentFromRect = '{{' + settings.presentFromRect[0] + ',' + settings.presentFromRect[1] + '},{' + settings.presentFromRect[2] + ',' + settings.presentFromRect[3] + '}}';
  45 + }
  46 + else {
  47 + console.log('PrintPlugin.print warning: presentFromRect option is invalid, expecting an array in the following format: [x, y, width, height].');
  48 + settings.presentFromRect = '{{0,0},{0,0}}';
  49 + }
  50 +
  51 + PhoneGap.exec(settings.success, settings.failure, 'PrintPlugin', 'print', [
  52 + settings.html,
  53 + PrintPlugin.UIPrintInfoOutputType[settings.outputType],
  54 + settings.jobName,
  55 + PrintPlugin.UIPrintInfoDuplex[settings.duplex],
  56 + PrintPlugin.UIPrintInfoOrientation[settings.orientation],
  57 + settings.showsPageRange,
  58 + settings.presentFromRect
  59 + ]);
  60 +};
  61 +
  62 +
  63 +PrintPlugin.prototype.isPrintingAvailable = function(settings) {
  64 + settings = PrintPlugin.defaults(settings, {
  65 + success: function() {},
  66 + failure: function() {}
  67 + });
  68 +
  69 + PhoneGap.exec(settings.success, settings.failure, 'PrintPlugin', 'isPrintingAvailable', []);
  70 +};
  71 +
  72 +
80 73 PhoneGap.addConstructor(function() {
81   - if(!window.plugins){
82   - window.plugins = {};
83   - }
84   - window.plugins.printPlugin = new PrintPlugin();
85   -});
  74 + if (!window.plugins) {
  75 + window.plugins = {};
  76 + }
  77 + window.plugins.printPlugin = new PrintPlugin();
  78 +});
  79 +
  80 +
  81 +
  82 +// Underscore.js 1.2.3
  83 +// (c) 2009-2011 Jeremy Ashkenas, DocumentCloud Inc.
  84 +// Underscore is freely distributable under the MIT license.
  85 +// Portions of Underscore are inspired or borrowed from Prototype,
  86 +// Oliver Steele's Functional, and John Resig's Micro-Templating.
  87 +// For all details and documentation:
  88 +// http://documentcloud.github.com/underscore
  89 +
  90 +// The cornerstone, an `each` implementation, aka `forEach`.
  91 +// Handles objects with the built-in `forEach`, arrays, and raw objects.
  92 +// Delegates to **ECMAScript 5**'s native `forEach` if available.
  93 +PrintPlugin.each = function(obj, iterator, context) {
  94 + if (obj == null) return;
  95 + if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
  96 + obj.forEach(iterator, context);
  97 + } else if (obj.length === +obj.length) {
  98 + for (var i = 0, l = obj.length; i < l; i++) {
  99 + if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
  100 + }
  101 + } else {
  102 + for (var key in obj) {
  103 + if (Object.prototype.hasOwnProperty.call(obj, key)) {
  104 + if (iterator.call(context, obj[key], key, obj) === breaker) return;
  105 + }
  106 + }
  107 + }
  108 +};
  109 +
  110 +// Fill in a given object with default properties.
  111 +PrintPlugin.defaults = function(obj) {
  112 + PrintPlugin.each(Array.prototype.slice.call(arguments, 1), function(source) {
  113 + for (var prop in source) {
  114 + if (obj[prop] == null) obj[prop] = source[prop];
  115 + }
  116 + });
  117 + return obj;
  118 +};
230 iPhone/PrintPlugin/PrintPlugin.m
@@ -10,148 +10,126 @@
10 10 #import "PrintPlugin.h"
11 11
12 12 @interface PrintPlugin (Private)
13   --(void) doPrint;
14   --(void) callbackWithFuntion:(NSString *)function withData:(NSString *)value;
15 13 - (BOOL) isPrintServiceAvailable;
16 14 @end
17 15
18 16 @implementation PrintPlugin
19 17
20   -@synthesize successCallback, failCallback, printHTML, dialogTopPos, dialogLeftPos;
21 18
22   -/*
23   - Is printing available. Callback returns true/false if printing is available/unavailable.
24   - */
25   -- (void) isPrintingAvailable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
26   - NSUInteger argc = [arguments count];
27   -
28   - if (argc < 1) {
29   - return;
  19 +- (void) isPrintingAvailable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
  20 +{
  21 + NSString* callbackID = [arguments pop];
  22 + PluginResult* result;
  23 +
  24 + if ([self isPrintServiceAvailable])
  25 + {
  26 + result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString:@"Printing is available."];
  27 + [self writeJavascript: [result toSuccessCallbackString:callbackID]];
  28 + return;
30 29 }
31   -
32   -
33   - NSString *callBackFunction = [arguments objectAtIndex:0];
34   - [self callbackWithFuntion:callBackFunction withData:
35   - [NSString stringWithFormat:@"{available: %@}", ([self isPrintServiceAvailable] ? @"true" : @"false")]];
36   -
  30 +
  31 + result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString:@"Device doesn't support printing or printing is not available."];
  32 + [self writeJavascript: [result toErrorCallbackString:callbackID]];
37 33 }
38 34
39 35 - (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
40   - NSUInteger argc = [arguments count];
41   -
42   - if (argc < 1) {
43   - return;
  36 + NSString* callbackID = [arguments pop];
  37 + VERIFY_ARGUMENTS(arguments, 7, callbackID)
  38 +
  39 + NSString* html = [arguments pop];
  40 + UIPrintInfoOutputType outputType = [[arguments pop] intValue];
  41 + NSString* jobName = [arguments pop];
  42 + UIPrintInfoDuplex duplex = [[arguments pop] intValue];
  43 + UIPrintInfoOrientation orientation = [[arguments pop] intValue];
  44 + BOOL showsPageRange = [[arguments pop] boolValue];
  45 + CGRect presentFromRect = CGRectFromString([arguments pop]);
  46 +
  47 +
  48 + __block PluginResult* result;
  49 +
  50 +
  51 + if (![self isPrintServiceAvailable])
  52 + {
  53 + result = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString:@"Device doesn't support printing or printing is not available."];
  54 + [self writeJavascript: [result toErrorCallbackString:callbackID]];
  55 + return;
44 56 }
45   - self.printHTML = [arguments objectAtIndex:0];
46   -
47   - if (argc >= 2){
48   - self.successCallback = [arguments objectAtIndex:1];
49   - }
50   -
51   - if (argc >= 3){
52   - self.failCallback = [arguments objectAtIndex:2];
53   - }
54   -
55   - if (argc >= 4){
56   - self.dialogLeftPos = [[arguments objectAtIndex:3] intValue];
57   - }
58   -
59   - if (argc >= 5){
60   - self.dialogTopPos = [[arguments objectAtIndex:4] intValue];
61   - }
62   -
63   -
64   -
65   -
66   - [self doPrint];
67 57
68   -}
  58 + UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];
  59 + if (!controller)
  60 + {
  61 + result = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString:@"Couldn't get shared UIPrintInteractionController!"];
  62 + [self writeJavascript: [result toErrorCallbackString:callbackID]];
  63 + return;
  64 + }
69 65
70   -- (void) doPrint{
71   - if (![self isPrintServiceAvailable]){
72   - [self callbackWithFuntion:self.failCallback withData: @"{success: false, available: false}"];
73   -
74   - return;
75   - }
76   -
77   - UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
78   -
79   - if (!controller){
80   - return;
81   - }
82   -
83   - if ([UIPrintInteractionController isPrintingAvailable]){
84   - //Set the priner settings
85   - UIPrintInfo *printInfo = [UIPrintInfo printInfo];
86   - printInfo.outputType = UIPrintInfoOutputGeneral;
87   - controller.printInfo = printInfo;
88   - controller.showsPageRange = YES;
89   -
90   -
91   - //Set the base URL to be the www directory.
92   - NSString *dbFilePath = [[NSBundle mainBundle] pathForResource:@"www" ofType:nil ];
93   - NSURL *baseURL = [NSURL fileURLWithPath:dbFilePath];
94   -
95   - //Load page into a webview and use its formatter to print the page
96   - UIWebView *webViewPrint = [[UIWebView alloc] init];
97   - [webViewPrint loadHTMLString:printHTML baseURL:baseURL];
98   -
99   - //Get formatter for web (note: margin not required - done in web page)
100   - UIViewPrintFormatter *viewFormatter = [webViewPrint viewPrintFormatter];
101   - controller.printFormatter = viewFormatter;
102   - controller.showsPageRange = YES;
103   -
104   -
105   - void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
106   - ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
107   - if (!completed || error) {
108   - [self callbackWithFuntion:self.failCallback withData:
109   - [NSString stringWithFormat:@"{success: false, available: true, error: \"%@\"}", error.localizedDescription]];
110   -
111   - [webViewPrint release];
112   -
113   - }
114   - else{
115   - [self callbackWithFuntion:self.successCallback withData: @"{success: true, available: true}"];
116   -
117   - [webViewPrint release];
118   - }
119   - };
120   -
121   - /*
122   - If iPad, and if button offsets passed, then show dilalog originating from offset
123   - */
124   - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
125   - dialogTopPos != 0 && dialogLeftPos != 0) {
126   - [controller presentFromRect:CGRectMake(self.dialogLeftPos, self.dialogTopPos, 0, 0) inView:self.webView animated:YES completionHandler:completionHandler];
127   - } else {
128   - [controller presentAnimated:YES completionHandler:completionHandler];
129   - }
130   - }
131   -}
  66 + UIPrintInfo* printInfo = [UIPrintInfo printInfo];
  67 + printInfo.outputType = outputType;
  68 + printInfo.duplex = duplex;
  69 + printInfo.orientation = orientation;
  70 +
  71 + if ([jobName length] > 0) {
  72 + printInfo.jobName = jobName;
  73 + }
  74 +
  75 + controller.printInfo = printInfo;
  76 + controller.showsPageRange = showsPageRange;
  77 +
  78 + //Set the base URL to be the www directory.
  79 + NSURL* baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"www" ofType:nil ]];
  80 +
  81 + //Load page into a webview and use its formatter to print the page
  82 + UIWebView *webViewPrint = [[UIWebView alloc] init];
  83 + webViewPrint.dataDetectorTypes = UIDataDetectorTypeNone;
  84 + [webViewPrint loadHTMLString:html baseURL:baseURL];
  85 +
  86 + UIViewPrintFormatter *viewFormatter = [webViewPrint viewPrintFormatter];
  87 + controller.printFormatter = viewFormatter;
  88 +
  89 + //Respond to Print-Job Completion and Errors
  90 + void (^completionHandler)(UIPrintInteractionController*, BOOL, NSError*) =
  91 + ^(UIPrintInteractionController* printController, BOOL completed, NSError* error)
  92 + {
  93 + if (!completed && error)
  94 + {
  95 + result = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString:error.localizedDescription];
  96 + [self writeJavascript: [result toErrorCallbackString:callbackID]];
  97 + }
  98 + else if (!completed)
  99 + {
  100 + result = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString:@"Print job not completed."];
  101 + [self writeJavascript: [result toErrorCallbackString:callbackID]];
  102 + }
  103 + else
  104 + {
  105 + result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString:@"Print job completed."];
  106 + [self writeJavascript: [result toSuccessCallbackString:callbackID]];
  107 + }
  108 + [webViewPrint release];
  109 + };
132 110
133   --(BOOL) isPrintServiceAvailable{
134   -
135   - Class myClass = NSClassFromString(@"UIPrintInteractionController");
136   - if (myClass) {
137   - UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
138   - return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
139   - }
140   -
141   -
142   - return NO;
  111 + //Show the Print dialog
  112 + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && !CGRectIsEmpty(presentFromRect))
  113 + {
  114 + [controller presentFromRect:presentFromRect inView:self.webView animated:YES completionHandler:completionHandler];
  115 + }
  116 + else
  117 + {
  118 + [controller presentAnimated:YES completionHandler:completionHandler];
  119 + }
143 120 }
144 121
145   -#pragma mark -
146   -#pragma mark Return messages
147   -
148   --(void) callbackWithFuntion:(NSString *)function withData:(NSString *)value{
149   - if (!function || [@"" isEqualToString:function]){
150   - return;
151   - }
152   -
153   - NSString* jsCallBack = [NSString stringWithFormat:@"%@(%@);", function, value];
154   - [self writeJavascript: jsCallBack];
  122 +
  123 +-(BOOL) isPrintServiceAvailable
  124 +{
  125 + Class printControllerClass = NSClassFromString(@"UIPrintInteractionController");
  126 + if (printControllerClass)
  127 + {
  128 + UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];
  129 + return controller && [UIPrintInteractionController isPrintingAvailable];
  130 + }
  131 +
  132 + return NO;
155 133 }
156 134
157 135 @end
130 iPhone/PrintPlugin/READEME.md
Source Rendered
... ... @@ -1,130 +0,0 @@
1   -# PhoneGap Print-Plugin #
2   -by Ian Tipton (github.com/itip).
3   -
4   -Print from iOS devices to AirPrint compatible printers.
5   -
6   -
7   -## Adding the Plugin to your project ##
8   -
9   -Using this plugin requires [iPhone PhoneGap](http://github.com/phonegap/phonegap-iphone).
10   -
11   -1. Make sure your PhoneGap Xcode project has been [updated for the iOS 4 SDK](http://wiki.phonegap.com/Upgrade-your-PhoneGap-Xcode-Template-for-iOS-4), and you are using PhoneGap version 1 or higher.
12   -2. Add the .h and .m files to your Plugins folder in your project
13   -3. Add the .js files to your "www" folder on disk, and add reference(s) to the .js files as <link> tags in your html file(s)
14   -4. Update the PhoneGap.plist file: Find PhoneGap.plist in your project, expand the "Plugins" section, click on "+" on the last line to add a new line. Add a new value with a key of 'printPlugin' and a value of 'PrintPlugin'
15   -5. Although printing is only supported on iOS 4.2+, if your app can be installed on earlier versions of iOS then see the 'Supporting devices running below iOS < 4.2' below.
16   -6. See the sample index.html file for an example use of the plugin.
17   -
18   -## RELEASE NOTES ##
19   -
20   -### 20110813 ###
21   -* Initial release
22   -* Allows the printing of an HTML string to AirPrint compatible printers.
23   -
24   -
25   -## Using the plugin ##
26   -
27   -The plugin creates the object window.plugins.pgPrint with two methods:
28   -
29   -### isPrintingAvailable ###
30   -
31   -Printing is only available on devices capable of multi-tasking (iPhone 3GS, iPhone 4 etc.) running iOS 4.2 or later. You can use this function to hide print functionality from users who will be unable to use it. Function takes a callback function, passed to which is a JSON object which contains a boolean property called available.
32   -
33   -```javascript
34   -/*
35   - Find out if printing is available. Use this for showing/hiding print buttons.
36   - */
37   - window.plugins.printPlugin.isPrintingAvailable(
38   - function(result){
39   - alert(result.available ? "Printing is available" : "Printing NOT available");
40   - }
41   - );
42   -```
43   -
44   -### print ###
45   -Function takes an html string and (optionally) a success callback, failure callback, and options.
46   -
47   -1. An HTML string, e.g. <strong>hello<strong>
48   -2. Success callback - receives an object with two parameters:
49   - - success (always true)
50   - - available (always true)
51   -3. Failure callback - receives an object with properties
52   - - success (always false)
53   - - available (false if printing not available on this device)
54   - - error (error message returned by iOS in the event of an error)
55   -4. An object which contains printing options (see below).
56   -
57   -```javascript
58   -//Get HTML string
59   -var html = document.getElementById("printHTML").innerHTML;
60   -
61   -/*
62   - Pass an HTML and - optionally - success function, error function.
63   - */
64   - window.plugins.printPlugin.print(
65   - html,
66   - function(result) {
67   - alert("Printing successful");
68   - },
69   - function(result) {
70   - if (!result.available){
71   - alert("Printing is not available");
72   - }
73   - else{
74   - //Localised error description
75   - alert(result.error);
76   - }
77   - }
78   - /*
79   - Add the following on an iPad to position the dialog
80   - ,
81   - {dialogOffset: {left: 500, top: 900}}
82   - */
83   - );
84   -```
85   -
86   -## LICENSE ##
87   -
88   -### Supporting devices running below iOS < 4.2 ###
89   -In order to compile this for versions of iOS earlier than 4.2 (when printing was introduced) then you will need to add -weak_framework UIKit to the project settings under "Other Linker Flags". See the Stack Overflow article for more information: http://stackoverflow.com/questions/4297723/ios-add-printing-but-keep-compatibility-with-ios-3.
90   -
91   -### Testing in the iOS Simulator ###
92   -There's no need to waste lots of paper when testing - if you're using the iOS simulator, select File->Open Printer Simulator to open some dummy printers (print outs will appear as PDF files).
93   -
94   -### Adding Page Breaks to Printouts ###
95   -Use the 'page-break-before' property to specify a page break, e.g.
96   -
97   -```javascript
98   -<p>
99   -First page.
100   -</p>
101   -
102   -<p style="page-break-before: always">
103   -Second page.
104   -</p>
105   -```
106   -
107   -See W3Schools for more more information: http://www.w3schools.com/cssref/pr_print_pagebb.asp
108   -
109   -Note: you will need to add an extra top margin to new pages.
110   -
111   -
112   -### Printing on Real Printers ###
113   -Printing is only supported on AirPrint-enabled printers or with the use of third-party software on your computer. The following pages contain more information:
114   - - AirPrint-enabled printers: http://www.apple.com/ipad/features/airprint.html
115   - - Enabling AirPrint on your computer: http://reviews.cnet.com/8301-19512_7-20023976-233.html, or http://www.ecamm.com/mac/printopia/
116   -
117   -
118   -## LICENSE ##
119   -
120   -The MIT License
121   -
122   -Copyright (c) 2011 Ian Tipton
123   -
124   -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
125   -
126   -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
127   -
128   -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
129   -
130   -
182 iPhone/PrintPlugin/README.md
Source Rendered
... ... @@ -0,0 +1,182 @@
  1 +# PhoneGap Print-Plugin #
  2 +by Ian Tipton (github.com/itip).
  3 +
  4 +Print from iOS devices to AirPrint compatible printers.
  5 +
  6 +
  7 +## Adding the Plugin to your project ##
  8 +
  9 +Using this plugin requires [iPhone PhoneGap](http://github.com/phonegap/phonegap-iphone).
  10 +
  11 +1. Make sure your PhoneGap Xcode project has been [updated for the iOS 4 SDK](http://wiki.phonegap.com/Upgrade-your-PhoneGap-Xcode-Template-for-iOS-4), and you are using PhoneGap version 1 or higher.
  12 +2. Add the .h and .m files to your Plugins folder in your project
  13 +3. Add the .js files to your "www" folder on disk, and add reference(s) to the .js files as <link> tags in your html file(s)
  14 +4. Update the PhoneGap.plist file: Find PhoneGap.plist in your project, expand the "Plugins" section, click on "+" on the last line to add a new line. Add a new value with a key of 'printPlugin' and a value of 'PrintPlugin'
  15 +5. Although printing is only supported on iOS 4.2+, if your app can be installed on earlier versions of iOS then see the 'Supporting devices running below iOS < 4.2' below.
  16 +6. See the sample index.html file for an example use of the plugin.
  17 +
  18 +## RELEASE NOTES ##
  19 +
  20 +### 20110813 ###
  21 +* Initial release
  22 +* Allows the printing of an HTML string to AirPrint compatible printers.
  23 +
  24 +
  25 +## Using the plugin ##
  26 +
  27 +The plugin creates the object window.plugins.printPlugin with two methods:
  28 +
  29 +### window.plugins.printPlugin.isPrintingAvailable( settings ) ###
  30 +__settings__ A set of key/value pairs that configure the AirPrint request. All settings are optional.
  31 +
  32 +* __success__<br />
  33 + default: function(result) {}
  34 +
  35 + A function to be called if the request succeeds. The function gets passed a status message.
  36 +
  37 +* __failure__<br />
  38 + default: function(result) {}
  39 +
  40 + A function to be called if the request fails. The function gets passed a status message.
  41 +
  42 +```javascript
  43 +window.plugins.printPlugin.isPrintingAvailable({
  44 + success: function() {
  45 + alert("Printing is available");
  46 + },
  47 + failure: function() {
  48 + alert("Printing NOT available");
  49 + }
  50 +});
  51 +```
  52 +
  53 +### window.plugins.printPlugin.print( settings ) ###
  54 +__settings__ A set of key/value pairs that configure the AirPrint request. All settings are optional.
  55 +
  56 +* __html__<br />
  57 + default: <empty string>
  58 +
  59 + An HTML string which will be loaded into it's own UIWebView for printing. Assets are all loaded using the same document root as PhoneGap.
  60 +
  61 +* __outputType__<br />
  62 + default: UIPrintInfoOutputGeneral
  63 +
  64 + The output type, which is an indication of the type of content the application is drawing or providing. Possible values are:
  65 + - UIPrintInfoOutputGeneral
  66 + - UIPrintInfoOutputPhoto
  67 + - UIPrintInfoOutputGrayscale
  68 +
  69 + http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPrintInfo_Class/Reference/Reference.html#//apple_ref/occ/instp/UIPrintInfo/outputType
  70 +
  71 +* __jobName__<br />
  72 + default: <empty string>
  73 +
  74 + The name of the print job is displayed in the Print Center when the job is printing. The default job name when an empty string is specified is the application name.
  75 +
  76 +* __duplex__<br />
  77 + default: UIPrintInfoDuplexLongEdge
  78 +
  79 + Some printers can print either duplex (double-sided) or single-sided. If double-sided is selected, a printer can either print flipping the back page along the long edge of the paper or along the short edge. Possible values are:
  80 + - UIPrintInfoDuplexNone
  81 + - UIPrintInfoDuplexLongEdge
  82 + - UIPrintInfoDuplexShortEdge
  83 +
  84 + http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPrintInfo_Class/Reference/Reference.html#//apple_ref/occ/instp/UIPrintInfo/duplex
  85 +
  86 +* __orientation__<br />
  87 + default: UIPrintInfoOrientationPortrait
  88 +
  89 + An application can set this property to a value thats appropriate to the printable content or it can put up a user interface that enables users to pick the printing orientation. Possible values are:
  90 + - UIPrintInfoOrientationPortrait
  91 + - UIPrintInfoOrientationLandscape
  92 +
  93 + http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPrintInfo_Class/Reference/Reference.html#//apple_ref/occ/instp/UIPrintInfo/orientation
  94 +
  95 +* __showsPageRange__<br />
  96 + default: true
  97 +
  98 + A Boolean value that determines whether the printing options include a page-range control.
  99 +
  100 +* __presentFromRect__<br />
  101 + default: [0,0,0,0]
  102 +
  103 + Presents the iPad printing user interface in a popover view that can be animated from any area in a view. Values specified as x, y, width, and height.
  104 +
  105 +* __success__<br />
  106 + default: function(result) {}
  107 +
  108 + A function to be called if the request succeeds. The function gets passed a status message.
  109 +
  110 +* __failure__<br />
  111 + default: function(result) {}
  112 +
  113 + A function to be called if the request fails. The function gets passed a status message.
  114 +
  115 +
  116 +```javascript
  117 +var el = $('#printBtn'), offset = el.offset(), data = $('#printContent').html();
  118 +
  119 +window.plugins.printPlugin.print({
  120 + html: data,
  121 + jobName: 'BaseballCardSummary',
  122 + duplex: 'UIPrintInfoDuplexShortEdge',
  123 + showsPageRange: false,
  124 + presentFromRect: [
  125 + offset.left,
  126 + offset.top,
  127 + el.outerWidth(),
  128 + el.outerHeight()
  129 + ],
  130 + success: function(result) {
  131 + alert('Document has successfully printed.');
  132 + }
  133 +});
  134 +```
  135 +
  136 +
  137 +## LICENSE ##
  138 +
  139 +### Supporting devices running below iOS < 4.2 ###
  140 +In order to compile this for versions of iOS earlier than 4.2 (when printing was introduced) then you will need to add -weak_framework UIKit to the project settings under "Other Linker Flags". See the Stack Overflow article for more information: http://stackoverflow.com/questions/4297723/ios-add-printing-but-keep-compatibility-with-ios-3.
  141 +
  142 +### Testing in the iOS Simulator ###
  143 +There's no need to waste lots of paper when testing - if you're using the iOS simulator, select File->Open Printer Simulator to open some dummy printers (print outs will appear as PDF files).
  144 +
  145 +If you have issues with the app crashing with `EXC_BAD_ACCESS` on iOS Simulator you may have a weak linking issue. With your project highlighted in the left column in XCode go to Targets > Your Project > Build Settings > Linking > Other Linker Flags and replace `-weak_library` with `-weak-lSystem`
  146 +For more information see: http://stackoverflow.com/questions/6738858/use-of-blocks-crashes-app-in-iphone-simulator-4-3-xcode-4-2-and-4-0-2
  147 +
  148 +### Adding Page Breaks to Printouts ###
  149 +Use the 'page-break-before' property to specify a page break, e.g.
  150 +
  151 +```javascript
  152 +<p>
  153 +First page.
  154 +</p>
  155 +
  156 +<p style="page-break-before: always">
  157 +Second page.
  158 +</p>
  159 +```
  160 +
  161 +See W3Schools for more more information: http://www.w3schools.com/cssref/pr_print_pagebb.asp
  162 +
  163 +Note: you will need to add an extra top margin to new pages.
  164 +
  165 +
  166 +### Printing on Real Printers ###
  167 +Printing is only supported on AirPrint-enabled printers or with the use of third-party software on your computer. The following pages contain more information:
  168 + - AirPrint-enabled printers: http://www.apple.com/ipad/features/airprint.html
  169 + - Enabling AirPrint on your computer: http://reviews.cnet.com/8301-19512_7-20023976-233.html, or http://www.ecamm.com/mac/printopia/
  170 +
  171 +
  172 +## LICENSE ##
  173 +
  174 +The MIT License
  175 +
  176 +Copyright (c) 2011 Ian Tipton
  177 +
  178 +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  179 +
  180 +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  181 +
  182 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
112 iPhone/PrintPlugin/index.html
@@ -6,54 +6,51 @@
6 6 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
7 7
8 8 <!-- iPad/iPhone specific css below, add after your main css >
9   - <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
10   - <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
  9 + <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
  10 + <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
11 11 -->
12 12 <!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
13   - <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
14   - <script type="text/javascript" charset="utf-8" src="PrintPlugin.js"></script>
15   -
  13 + <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
  14 + <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
  15 + <script type="text/javascript" charset="utf-8" src="PrintPlugin.js"></script>
  16 +
16 17 <script type="text/javascript" charset="utf-8">
17 18
18 19 function printIt(){
19   -
20   - //Get HTML string
21   - var html = document.getElementById("printHTML").innerHTML;
22   -
23   - /*
24   - Pass a DOM node (or HTML string) and - optionally - success function, error function, position of print dialog (iPad only).
25   - */
26   - window.plugins.printPlugin.print(html,
27   - function(result) {
28   - alert("Printing successful");
29   - },
30   - function(result) {
31   - if (!result.available){
32   - alert("Printing is not available");
33   - }
34   - else{
35   - //Localised error description
36   - alert(result.error);
37   - }
38   - }
39   - /*
40   - Add the following on an iPad to position the dialog
41   - ,
42   - {dialogOffset: {left: 500, top: 900}}
43   - */
44   - );
  20 + var el = $('button'), offset = el.offset(), data = $('#printHTML').html();
  21 +
  22 + window.plugins.printPlugin.print({
  23 + html: data,
  24 + jobName: 'BaseballCardSummary',
  25 + duplex: 'UIPrintInfoDuplexShortEdge',
  26 + showsPageRange: false,
  27 + success: function(result) {
  28 + alert('Document has successfully printed.');
  29 + },
  30 + failure: function(result) {
  31 + alert(result);
  32 + },
  33 +
  34 + // this will only be used on the iPad
  35 + presentFromRect: [
  36 + offset.left,
  37 + offset.top,
  38 + el.outerWidth(),
  39 + el.outerHeight()
  40 + ]
  41 + });
45 42 }
46   -
47   -
  43 +
  44 +
48 45 // If you want to prevent dragging, uncomment this section
49 46 /*
50   - function preventBehavior(e)
51   - {
52   - e.preventDefault();
  47 + function preventBehavior(e)
  48 + {
  49 + e.preventDefault();
53 50 };
54 51 document.addEventListener("touchmove", preventBehavior, false);
55 52 */
56   -
  53 +
57 54 /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
58 55 see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
59 56 for more details -jm */
@@ -63,12 +60,12 @@
63 60 // TODO: do something with the url passed in.
64 61 }
65 62 */
66   -
  63 +
67 64 function onBodyLoad()
68   - {
  65 + {
69 66 document.addEventListener("deviceready",onDeviceReady,false);
70 67 }
71   -
  68 +
72 69 /* When this function is called, PhoneGap has been initialized and is ready to roll */
73 70 /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
74 71 see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
@@ -78,40 +75,43 @@
78 75 // do your thing!
79 76 //navigator.notification.alert("PhoneGap is working")
80 77 /*
81   - Find out if printing is available. Use this for showing/hiding print buttons.
  78 + Find out if printing is available. Use this for showing/hiding print buttons.
82 79 */
83   - window.plugins.printPlugin.isPrintingAvailable(
84   - function(result){
85   - alert(result.available ? "Printing is available" : "Printing NOT available");
  80 + window.plugins.printPlugin.isPrintingAvailable({
  81 + success: function() {
  82 + alert("Printing is available");
  83 + },
  84 + failure: function() {
  85 + alert("Printing NOT available");
86 86 }
87   - );
  87 + });
88 88 }
89   -
  89 +
90 90 </script>
91 91 </head>
92 92 <body onload="onBodyLoad()">
93 93 <h1>Printing</h1>
94   -
  94 +
95 95 <button onclick="printIt()">Print the HTML below</button>
96   -
  96 +
97 97 <br /><br /><br />
98   -
  98 +
99 99 <div id="printHTML">
100   -
  100 +
101 101 <div style="color: red;">
102 102 <p>Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled 'ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it.</p>
103 103 </div>
104   -
  104 +
105 105 <div style="color: green; page-break-before:always;">
106   - <p>In the Propontis, as far as I can learn, none of that peculiar substance called BRIT is to be found, the aliment of the right whale. But I have every reason to believe that the food of the sperm whale&mdash;squid or cuttle-fish&mdash;lurks at the bottom of that sea, because large creatures, but by no means the largest of that sort, have been found at its surface. If, then, you properly put these statements together, and reason upon them a bit, you will clearly perceive that, according to all human reasoning, Procopius's sea-monster, that for half a century stove the ships of a Roman Emperor, must in all probability have been a sperm whale.</p>
  106 + <p>In the Propontis, as far as I can learn, none of that peculiar substance called BRIT is to be found, the aliment of the right whale. But I have every reason to believe that the food of the sperm whale&mdash;squid or cuttle-fish&mdash;lurks at the bottom of that sea, because large creatures, but by no means the largest of that sort, have been found at its surface. If, then, you properly put these statements together, and reason upon them a bit, you will clearly perceive that, according to all human reasoning, Procopius's sea-monster, that for half a century stove the ships of a Roman Emperor, must in all probability have been a sperm whale.</p>
107 107 </div>
108   -
109   -
  108 +
  109 +
110 110 <div style="color: blue; page-break-before:always;">
111 111 <p>They were not their families, nor their wives, nor their servants; the relationship was peculiar, and so unlike anything known to us that it is most difficult to describe. All property among the green Martians is owned in common by the community, except the personal weapons, ornaments and sleeping silks and furs of the individuals. These alone can one claim undisputed right to, nor may he accumulate more of these than are required for his actual needs. The surplus he holds merely as custodian, and it is passed on to the younger members of the community as necessity demands.</p>
112   -
  112 +
113 113 </div>
114   -
  114 +
115 115 </div>
116 116 </body>
117 117 </html>

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.