2013-12-11 20:00:16 +08:00
|
|
|
/*
|
2014-09-08 04:52:36 +08:00
|
|
|
Copyright 2013-2014 appPlant UG
|
2013-12-11 20:21:23 +08:00
|
|
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
or more contributor license agreements. See the NOTICE file
|
|
|
|
distributed with this work for additional information
|
|
|
|
regarding copyright ownership. The ASF licenses this file
|
|
|
|
to you under the Apache License, Version 2.0 (the
|
|
|
|
"License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
|
|
software distributed under the License is distributed on an
|
|
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
KIND, either express or implied. See the License for the
|
|
|
|
specific language governing permissions and limitations
|
|
|
|
under the License.
|
|
|
|
*/
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-10 22:13:04 +08:00
|
|
|
#import "APPPrinter.h"
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
@interface APPPrinter ()
|
2013-08-11 17:37:59 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
@property (retain) NSString* callbackId;
|
2013-08-11 17:37:59 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2013-08-10 22:13:04 +08:00
|
|
|
@implementation APPPrinter
|
2013-08-10 00:58:47 +08:00
|
|
|
|
|
|
|
/*
|
2014-09-08 04:52:36 +08:00
|
|
|
* Checks if the printing service is available.
|
|
|
|
*
|
|
|
|
* @param {Function} callback
|
|
|
|
* A callback function to be called with the result
|
2013-08-10 00:58:47 +08:00
|
|
|
*/
|
2014-09-08 04:52:36 +08:00
|
|
|
- (void) isAvailable:(CDVInvokedUrlCommand*)command
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2013-08-13 21:39:22 +08:00
|
|
|
CDVPluginResult* pluginResult;
|
2014-09-08 04:52:36 +08:00
|
|
|
BOOL isAvailable = [self isPrintingAvailable];
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
2014-09-08 04:52:36 +08:00
|
|
|
messageAsBool:isAvailable];
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
[self.commandDelegate sendPluginResult:pluginResult
|
|
|
|
callbackId:command.callbackId];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
2014-09-08 04:52:36 +08:00
|
|
|
* Sends the printing content to the printer controller and opens them.
|
|
|
|
*
|
|
|
|
* @param {NSString} content
|
|
|
|
* The (HTML encoded) content
|
2013-08-10 15:50:42 +08:00
|
|
|
*/
|
2013-12-11 20:21:23 +08:00
|
|
|
- (void) print:(CDVInvokedUrlCommand*)command
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2014-09-08 04:52:36 +08:00
|
|
|
if (!self.isPrintingAvailable) {
|
2013-08-10 00:58:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
_callbackId = command.callbackId;
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
NSArray* arguments = [command arguments];
|
|
|
|
NSString* content = [arguments objectAtIndex:0];
|
|
|
|
NSMutableDictionary* settings = [arguments objectAtIndex:1];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
UIPrintInteractionController* controller = [self printController];
|
2013-12-11 20:21:23 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
[self adjustPrintController:controller withSettings:settings];
|
|
|
|
[self loadContent:content intoPrintController:controller];
|
|
|
|
[self presentPrintController:controller];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
2014-09-08 04:52:36 +08:00
|
|
|
* Retrieves an instance of shared print controller.
|
|
|
|
*
|
|
|
|
* @return {UIPrintInteractionController*}
|
2013-08-10 15:50:42 +08:00
|
|
|
*/
|
2014-09-08 04:52:36 +08:00
|
|
|
- (UIPrintInteractionController*) printController
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2013-08-13 21:39:22 +08:00
|
|
|
return [UIPrintInteractionController sharedPrintController];
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
/**
|
2014-09-08 04:52:36 +08:00
|
|
|
* Adjusts the settings for the print controller.
|
|
|
|
*
|
|
|
|
* @param {UIPrintInteractionController} controller
|
|
|
|
* The print controller instance
|
|
|
|
*
|
|
|
|
* @return {UIPrintInteractionController} controller
|
|
|
|
* The modified print controller instance
|
2013-08-13 21:39:22 +08:00
|
|
|
*/
|
2014-09-08 04:52:36 +08:00
|
|
|
- (UIPrintInteractionController*) adjustPrintController:(UIPrintInteractionController*)controller
|
|
|
|
withSettings:(NSMutableDictionary*)settings
|
2013-08-13 21:39:22 +08:00
|
|
|
{
|
2014-09-08 04:52:36 +08:00
|
|
|
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
|
|
|
|
UIPrintInfoOrientation orientation = UIPrintInfoOrientationPortrait;
|
|
|
|
UIPrintInfoOutputType outputType = UIPrintInfoOutputGeneral;
|
|
|
|
|
|
|
|
if ([[settings objectForKey:@"landscape"] boolValue]) {
|
|
|
|
orientation = UIPrintInfoOrientationLandscape;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([[settings objectForKey:@"graystyle"] boolValue]) {
|
|
|
|
outputType = UIPrintInfoOutputGrayscale;
|
|
|
|
}
|
|
|
|
|
|
|
|
printInfo.outputType = outputType;
|
|
|
|
printInfo.orientation = orientation;
|
|
|
|
printInfo.jobName = [settings objectForKey:@"name"];
|
|
|
|
printInfo.duplex = [[settings objectForKey:@"duplex"] boolValue];
|
|
|
|
printInfo.printerID = [settings objectForKey:@"printerId"];
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
controller.printInfo = printInfo;
|
2014-09-08 04:52:36 +08:00
|
|
|
controller.showsPageRange = NO;
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
return controller;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
/**
|
2014-09-08 04:52:36 +08:00
|
|
|
* Adjusts the web view and page renderer.
|
|
|
|
*/
|
|
|
|
- (void) adjustWebView:(UIWebView*)page
|
|
|
|
andPrintPageRenderer:(UIPrintPageRenderer*)renderer
|
|
|
|
{
|
|
|
|
UIViewPrintFormatter* formatter = [page viewPrintFormatter];
|
|
|
|
// margin not required - done in web page
|
|
|
|
formatter.contentInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
renderer.headerHeight = -30.0f;
|
|
|
|
renderer.footerHeight = -30.0f;
|
|
|
|
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
|
|
|
|
|
|
|
|
page.scalesPageToFit = YES;
|
|
|
|
page.dataDetectorTypes = UIDataDetectorTypeNone;
|
|
|
|
page.userInteractionEnabled = NO;
|
|
|
|
page.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
|
|
|
|
UIViewAutoresizingFlexibleHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the content into the print controller.
|
|
|
|
*
|
|
|
|
* @param {NSString} content
|
|
|
|
* The (HTML encoded) content
|
|
|
|
* @param {UIPrintInteractionController} controller
|
|
|
|
* The print controller instance
|
2013-08-13 21:39:22 +08:00
|
|
|
*/
|
2013-12-11 20:21:23 +08:00
|
|
|
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
|
2013-08-13 21:39:22 +08:00
|
|
|
{
|
2014-09-08 04:52:36 +08:00
|
|
|
UIWebView* page = [[UIWebView alloc] init];
|
|
|
|
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
|
|
|
|
|
|
|
|
[self adjustWebView:page andPrintPageRenderer:renderer];
|
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
// Set the base URL to be the www directory.
|
2014-09-08 04:52:36 +08:00
|
|
|
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
|
|
|
|
ofType:nil];
|
|
|
|
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
[page loadHTMLString:content baseURL:baseURL];
|
2013-08-13 21:39:22 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
controller.printPageRenderer = renderer;
|
2013-08-13 21:39:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 20:38:01 +08:00
|
|
|
/**
|
2014-09-08 04:52:36 +08:00
|
|
|
* Opens the print controller so that the user can choose between
|
|
|
|
* available iPrinters.
|
|
|
|
*
|
|
|
|
* @param {UIPrintInteractionController} controller
|
|
|
|
* The prepared print controller with a content
|
2013-12-11 20:38:01 +08:00
|
|
|
*/
|
2014-09-08 04:52:36 +08:00
|
|
|
- (void) presentPrintController:(UIPrintInteractionController*)controller
|
2013-12-11 20:38:01 +08:00
|
|
|
{
|
2014-09-08 04:52:36 +08:00
|
|
|
[controller presentAnimated:YES completionHandler:
|
|
|
|
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
|
|
|
|
CDVPluginResult* pluginResult =
|
|
|
|
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
|
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:pluginResult
|
|
|
|
callbackId:_callbackId];
|
|
|
|
}];
|
2013-12-11 20:38:01 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
2014-09-08 04:52:36 +08:00
|
|
|
* Checks either the printing service is avaible or not.
|
|
|
|
*
|
|
|
|
* @return {BOOL}
|
2013-08-10 15:50:42 +08:00
|
|
|
*/
|
2014-09-08 04:52:36 +08:00
|
|
|
- (BOOL) isPrintingAvailable
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2014-09-08 04:52:36 +08:00
|
|
|
Class controllerCls = NSClassFromString(@"UIPrintInteractionController");
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
if (!controllerCls) {
|
|
|
|
return NO;
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
return [self printController] && [UIPrintInteractionController
|
|
|
|
isPrintingAvailable];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
@end
|