Printer Plugin für iOS funktioniert für cordova 3.x
This commit is contained in:
parent
68221a38da
commit
244ada61bc
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
<config-file target="config.xml" parent="/*">
|
<config-file target="config.xml" parent="/*">
|
||||||
<feature name="Printer">
|
<feature name="Printer">
|
||||||
<param name="ios-package" value="CDVPlugin"/>
|
<param name="ios-package" value="CDVPrinter"/>
|
||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
|
|
||||||
|
@ -1,38 +1,33 @@
|
|||||||
//
|
/**
|
||||||
// PrintPlugin.h
|
* Printer.m
|
||||||
// Print Plugin
|
* Cordova Printer Plugin
|
||||||
//
|
*
|
||||||
// Created by Ian Tipton (github.com/itip) on 02/07/2011.
|
* Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
|
||||||
// Copyright 2011 Ian Tipton. All rights reserved.
|
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
||||||
// MIT licensed
|
* GPL v2 licensed
|
||||||
//
|
*/
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <Cordova/CDVPlugin.h>
|
#import <Cordova/CDVPlugin.h>
|
||||||
|
|
||||||
|
|
||||||
@interface CDVPrinter : CDVPlugin {
|
@interface CDVPrinter : CDVPlugin {
|
||||||
NSString* successCallback;
|
|
||||||
NSString* failCallback;
|
|
||||||
NSString* printHTML;
|
|
||||||
|
|
||||||
//Options
|
|
||||||
NSInteger dialogLeftPos;
|
|
||||||
NSInteger dialogTopPos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@property (nonatomic, copy) NSString* successCallback;
|
// Prints the content
|
||||||
@property (nonatomic, copy) NSString* failCallback;
|
- (void) print:(CDVInvokedUrlCommand*)command;
|
||||||
@property (nonatomic, copy) NSString* printHTML;
|
// Find out whether printing is supported on this platform
|
||||||
|
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command;
|
||||||
//Print Settings
|
|
||||||
@property NSInteger dialogLeftPos;
|
@end
|
||||||
@property NSInteger dialogTopPos;
|
|
||||||
|
|
||||||
//Print HTML
|
@interface CDVPrinter (Private)
|
||||||
- (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
|
|
||||||
|
// Bereitet den Drucker-Kontroller vor
|
||||||
//Find out whether printing is supported on this platform.
|
- (UIPrintInteractionController *) prepareController:(NSString*)content;
|
||||||
- (void) isPrintingAvailable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
|
// Überprüft, ob der Drucker-Dienst verfügbar ist
|
||||||
|
- (BOOL) isPrintServiceAvailable;
|
||||||
|
|
||||||
@end
|
@end
|
@ -1,136 +1,119 @@
|
|||||||
//
|
/**
|
||||||
// PrintPlugin.m
|
* Printer.m
|
||||||
// Print Plugin
|
* Cordova Printer Plugin
|
||||||
//
|
*
|
||||||
// Created by Ian Tipton (github.com/itip) on 02/07/2011.
|
* Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
|
||||||
// Copyright 2011 Ian Tipton. All rights reserved.
|
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
||||||
// MIT licensed
|
* GPL v2 licensed
|
||||||
//
|
*/
|
||||||
|
|
||||||
#import "CDVPrinter.h"
|
#import "CDVPrinter.h"
|
||||||
|
|
||||||
@interface CDVPrinter (Private)
|
|
||||||
-(void) doPrint;
|
|
||||||
-(void) callbackWithFuntion:(NSString *)function withData:(NSString *)value;
|
|
||||||
- (BOOL) isPrintServiceAvailable;
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation CDVPrinter
|
@implementation CDVPrinter
|
||||||
|
|
||||||
@synthesize successCallback, failCallback, printHTML, dialogTopPos, dialogLeftPos;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Is printing available. Callback returns true/false if printing is available/unavailable.
|
* Is printing available.
|
||||||
|
* Callback returns true/false if printing is available/unavailable.
|
||||||
*/
|
*/
|
||||||
- (void) isPrintingAvailable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
|
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command {
|
||||||
NSUInteger argc = [arguments count];
|
CDVPluginResult *pluginResult = nil;
|
||||||
|
|
||||||
if (argc < 1) {
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
||||||
return;
|
messageAsBool:[self isPrintServiceAvailable]];
|
||||||
|
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Öffnet den Drucker-Kontroller zur Auswahl des Druckers.
|
||||||
|
* Callback gibt Meta-Informationen an.
|
||||||
|
*/
|
||||||
|
- (void) print:(CDVInvokedUrlCommand*)command {
|
||||||
|
NSArray *arguments = [command arguments];
|
||||||
|
CDVPluginResult *pluginResult = nil;
|
||||||
|
|
||||||
NSString *callBackFunction = [arguments objectAtIndex:0];
|
|
||||||
[self callbackWithFuntion:callBackFunction withData:
|
|
||||||
[NSString stringWithFormat:@"{available: %@}", ([self isPrintServiceAvailable] ? @"true" : @"false")]];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
|
|
||||||
NSUInteger argc = [arguments count];
|
|
||||||
|
|
||||||
if (argc < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.printHTML = [arguments objectAtIndex:0];
|
|
||||||
|
|
||||||
if (argc >= 2){
|
|
||||||
self.successCallback = [arguments objectAtIndex:1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc >= 3){
|
|
||||||
self.failCallback = [arguments objectAtIndex:2];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc >= 4){
|
|
||||||
self.dialogLeftPos = [[arguments objectAtIndex:3] intValue];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc >= 5){
|
|
||||||
self.dialogTopPos = [[arguments objectAtIndex:4] intValue];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[self doPrint];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) doPrint{
|
|
||||||
if (![self isPrintServiceAvailable]) {
|
if (![self isPrintServiceAvailable]) {
|
||||||
[self callbackWithFuntion:self.failCallback withData: @"{success: false, available: false}"];
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
||||||
|
messageAsString:@"{success: false, available: false}"];
|
||||||
|
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ([arguments count] == 0) {
|
||||||
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
||||||
|
messageAsString:@"{success: false, available: true}"];
|
||||||
|
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *content = [arguments objectAtIndex:0];
|
||||||
|
UIPrintInteractionController *controller = [self prepareController:content];
|
||||||
|
|
||||||
|
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
|
||||||
|
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
|
||||||
|
CDVPluginResult *pluginResult = nil;
|
||||||
|
|
||||||
|
if (!completed || error) {
|
||||||
|
NSString *result = [NSString stringWithFormat:@"{success: false, available: true, error: \"%@\"}", error.localizedDescription];
|
||||||
|
|
||||||
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
||||||
|
messageAsString:result];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
||||||
|
messageAsString:@"{success: true, available: true}"];
|
||||||
|
}
|
||||||
|
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
|
};
|
||||||
|
|
||||||
|
[controller presentAnimated:YES completionHandler:completionHandler];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bereitet den Drucker-Kontroller vor.
|
||||||
|
*
|
||||||
|
* @param {NSString} content Der zu druckende Inhalt
|
||||||
|
*/
|
||||||
|
- (UIPrintInteractionController *) prepareController:(NSString *)content {
|
||||||
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
|
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
|
||||||
|
|
||||||
if (!controller){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ([UIPrintInteractionController isPrintingAvailable]){
|
|
||||||
//Set the priner settings
|
//Set the priner settings
|
||||||
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
|
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
|
||||||
printInfo.outputType = UIPrintInfoOutputGeneral;
|
printInfo.outputType = UIPrintInfoOutputGeneral;
|
||||||
controller.printInfo = printInfo;
|
controller.printInfo = printInfo;
|
||||||
controller.showsPageRange = YES;
|
controller.showsPageRange = YES;
|
||||||
|
|
||||||
|
|
||||||
//Set the base URL to be the www directory.
|
//Set the base URL to be the www directory.
|
||||||
NSString *dbFilePath = [[NSBundle mainBundle] pathForResource:@"www" ofType:nil ];
|
NSString *wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www" ofType:nil ];
|
||||||
NSURL *baseURL = [NSURL fileURLWithPath:dbFilePath];
|
NSURL *baseURL = [NSURL fileURLWithPath:wwwFilePath];
|
||||||
|
|
||||||
//Load page into a webview and use its formatter to print the page
|
//Load page into a webview and use its formatter to print the page
|
||||||
UIWebView *webViewPrint = [[UIWebView alloc] init];
|
UIWebView *webViewPrint = [[UIWebView alloc] init];
|
||||||
[webViewPrint loadHTMLString:printHTML baseURL:baseURL];
|
[webViewPrint loadHTMLString:content baseURL:baseURL];
|
||||||
|
|
||||||
//Get formatter for web (note: margin not required - done in web page)
|
//Get formatter for web (note: margin not required - done in web page)
|
||||||
UIViewPrintFormatter *viewFormatter = [webViewPrint viewPrintFormatter];
|
UIViewPrintFormatter *viewFormatter = [webViewPrint viewPrintFormatter];
|
||||||
controller.printFormatter = viewFormatter;
|
controller.printFormatter = viewFormatter;
|
||||||
controller.showsPageRange = YES;
|
controller.showsPageRange = YES;
|
||||||
|
|
||||||
|
return controller;
|
||||||
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
|
|
||||||
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
|
|
||||||
if (!completed || error) {
|
|
||||||
[self callbackWithFuntion:self.failCallback withData:
|
|
||||||
[NSString stringWithFormat:@"{success: false, available: true, error: \"%@\"}", error.localizedDescription]];
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
[self callbackWithFuntion:self.successCallback withData: @"{success: true, available: true}"];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
If iPad, and if button offsets passed, then show dilalog originating from offset
|
* Überprüft, ob der Drucker-Dienst verfügbar ist.
|
||||||
*/
|
*/
|
||||||
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
|
|
||||||
dialogTopPos != 0 && dialogLeftPos != 0) {
|
|
||||||
[controller presentFromRect:CGRectMake(self.dialogLeftPos, self.dialogTopPos, 0, 0) inView:self.webView animated:YES completionHandler:completionHandler];
|
|
||||||
} else {
|
|
||||||
[controller presentAnimated:YES completionHandler:completionHandler];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL) isPrintServiceAvailable {
|
- (BOOL) isPrintServiceAvailable {
|
||||||
|
Class printController = NSClassFromString(@"UIPrintInteractionController");
|
||||||
|
|
||||||
Class myClass = NSClassFromString(@"UIPrintInteractionController");
|
if (printController) {
|
||||||
if (myClass) {
|
|
||||||
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
|
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
|
||||||
|
|
||||||
return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
|
return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,16 +121,4 @@
|
|||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
|
||||||
#pragma mark Return messages
|
|
||||||
|
|
||||||
-(void) callbackWithFuntion:(NSString *)function withData:(NSString *)value{
|
|
||||||
if (!function || [@"" isEqualToString:function]){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NSString* jsCallBack = [NSString stringWithFormat:@"%@(%@);", function, value];
|
|
||||||
[self writeJavascript: jsCallBack];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
110
www/printer.js
110
www/printer.js
@ -1,86 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* Printer.m
|
||||||
|
* Cordova Printer Plugin
|
||||||
|
*
|
||||||
|
* Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
|
||||||
|
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
||||||
|
* GPL v2 licensed
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Printer = function () {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
Printer.prototype = {
|
||||||
|
/**
|
||||||
|
* Überprüft, ob der Drucker-Dienst verfügbar ist.
|
||||||
|
*
|
||||||
|
* @param {Function} callback
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
isServiceAvailable: function (callback) {
|
||||||
|
cordova.exec(callback, null, 'Printer', 'isServiceAvailable', []);
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Printer Plugin
|
* Übergibt den HTML-Content an den Drucker-Dienst.
|
||||||
* Copyright (c) 2011 Ian Tipton (github.com/itip)
|
*
|
||||||
* MIT licensed
|
* @param {String} content html string or DOM node (if latter, innerHTML is used to get the contents)
|
||||||
|
* @param {Function?} success callback function called if print successful. {success: true}
|
||||||
|
* @param {Function?} failure callback function called if print unsuccessful. If print fails, {error: reason}. If printing not available: {available: false}
|
||||||
*/
|
*/
|
||||||
|
print: function (content, success, failure) {
|
||||||
|
content = content.innerHTML || content;
|
||||||
|
|
||||||
var PrintPlugin = function() {
|
if (typeof content != 'string') {
|
||||||
|
console.log('Print function requires an HTML string. Not an object');
|
||||||
}
|
|
||||||
|
|
||||||
PrintPlugin.prototype.callbackMap = {};
|
|
||||||
PrintPlugin.prototype.callbackIdx = 0;
|
|
||||||
|
|
||||||
/*
|
|
||||||
print - html string or DOM node (if latter, innerHTML is used to get the contents). REQUIRED.
|
|
||||||
success - callback function called if print successful. {success: true}
|
|
||||||
fail - callback function called if print unsuccessful. If print fails, {error: reason}. If printing not available: {available: false}
|
|
||||||
options - {dialogOffset:{left: 0, right: 0}}. Position of popup dialog (iPad only).
|
|
||||||
*/
|
|
||||||
PrintPlugin.prototype.print = function(printHTML, success, fail, options) {
|
|
||||||
if (typeof printHTML != 'string'){
|
|
||||||
console.log("Print function requires an HTML string. Not an object");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cordova.exec(success, failure, 'Printer', 'print', [content]);
|
||||||
//var printHTML = "";
|
|
||||||
|
|
||||||
var dialogLeftPos = 0;
|
|
||||||
var dialogTopPos = 0;
|
|
||||||
|
|
||||||
|
|
||||||
if (options){
|
|
||||||
if (options.dialogOffset){
|
|
||||||
if (options.dialogOffset.left){
|
|
||||||
dialogLeftPos = options.dialogOffset.left;
|
|
||||||
if (isNaN(dialogLeftPos)){
|
|
||||||
dialogLeftPos = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (options.dialogOffset.top){
|
|
||||||
dialogTopPos = options.dialogOffset.top;
|
|
||||||
if (isNaN(dialogTopPos)){
|
|
||||||
dialogTopPos = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var key = 'print' + this.callbackIdx++;
|
|
||||||
window.plugins.printPlugin.callbackMap[key] = {
|
|
||||||
success: function(result) {
|
|
||||||
delete window.plugins.printPlugin.callbackMap[key];
|
|
||||||
success(result);
|
|
||||||
},
|
|
||||||
fail: function(result) {
|
|
||||||
delete window.plugins.printPlugin.callbackMap[key];
|
|
||||||
fail(result);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var callbackPrefix = 'window.plugins.printPlugin.callbackMap.' + key;
|
var printer = new Printer();
|
||||||
return cordova.exec("PrintPlugin.print", printHTML, callbackPrefix + '.success', callbackPrefix + '.fail', dialogLeftPos, dialogTopPos);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
module.exports = printer;
|
||||||
* Callback function returns {available: true/false}
|
|
||||||
*/
|
|
||||||
PrintPlugin.prototype.isPrintingAvailable = function(callback) {
|
|
||||||
var key = 'isPrintingAvailable' + this.callbackIdx++;
|
|
||||||
window.plugins.printPlugin.callbackMap[key] = function(result) {
|
|
||||||
delete window.plugins.printPlugin.callbackMap[key];
|
|
||||||
callback(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
var callbackName = 'window.plugins.printPlugin.callbackMap.' + key;
|
|
||||||
cordova.exec("PrintPlugin.isPrintingAvailable", callbackName);
|
|
||||||
};
|
|
||||||
|
|
||||||
cordova.addConstructor(function() {
|
|
||||||
if(!window.plugins){
|
|
||||||
window.plugins = {};
|
|
||||||
}
|
|
||||||
window.plugins.printPlugin = new PrintPlugin();
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user