2013-12-11 20:00:16 +08:00
|
|
|
/*
|
2016-07-21 03:00:33 +08:00
|
|
|
Copyright 2013-2016 appPlant GmbH
|
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"
|
2014-10-08 03:20:09 +08:00
|
|
|
#import <Cordova/CDVAvailability.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;
|
2016-02-29 20:05:56 +08:00
|
|
|
@property (retain) NSMutableDictionary* settings;
|
2018-12-21 19:47:34 +08:00
|
|
|
// this is used to cache the uiprinter making repeated prints faster
|
|
|
|
@property (nonatomic) UIPrinter *previousPrinter;
|
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
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Interface
|
|
|
|
|
2013-08-10 00:58:47 +08:00
|
|
|
/*
|
2014-09-08 04:52:36 +08:00
|
|
|
* Checks if the printing service is available.
|
2013-08-10 00:58:47 +08:00
|
|
|
*/
|
2016-08-03 23:20:10 +08:00
|
|
|
- (void) check:(CDVInvokedUrlCommand*)command
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2014-09-12 16:53:44 +08:00
|
|
|
[self.commandDelegate runInBackground:^{
|
|
|
|
CDVPluginResult* pluginResult;
|
2016-07-24 19:49:23 +08:00
|
|
|
BOOL isAvailable = [self isPrintingAvailable];
|
2016-07-27 20:24:48 +08:00
|
|
|
NSArray *multipart = @[[NSNumber numberWithBool:isAvailable],
|
|
|
|
[NSNumber numberWithInt:-1]];
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-09-12 16:53:44 +08:00
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
2016-07-24 19:49:23 +08:00
|
|
|
messageAsMultipart:multipart];
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-09-12 16:53:44 +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.
|
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
|
|
|
{
|
2018-12-21 19:47:34 +08:00
|
|
|
if (!self.isPrintingAvailable) return;
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
UIPrintInteractionController* controller = [self printController];
|
|
|
|
NSString* content = command.arguments[0];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
_settings = command.arguments[1];
|
|
|
|
_callbackId = command.callbackId;
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
[self adjustPrintController:controller
|
|
|
|
withSettings:_settings];
|
2013-12-11 20:21:23 +08:00
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
[self loadContent:content intoPrintController:controller];
|
2016-02-29 20:05:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
/**
|
2018-12-21 19:47:34 +08:00
|
|
|
* Displays system interface for selecting a printer.
|
2016-07-25 21:04:20 +08:00
|
|
|
*/
|
|
|
|
- (void) pick:(CDVInvokedUrlCommand*)command
|
|
|
|
{
|
2018-12-21 19:47:34 +08:00
|
|
|
if (!self.isPrintingAvailable) return;
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
NSMutableDictionary* settings = command.arguments[0];
|
|
|
|
NSArray* bounds = settings[@"bounds"];
|
|
|
|
_callbackId = command.callbackId;
|
|
|
|
CGRect rect;
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if (bounds) {
|
|
|
|
rect = [self convertIntoRect:bounds];
|
|
|
|
} else {
|
|
|
|
rect = CGRectMake(40, 30, 0, 0);
|
2018-12-17 18:33:28 +08:00
|
|
|
}
|
2018-12-21 19:47:34 +08:00
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
[self presentPrinterPicker:rect];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark UIWebViewDelegate
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sent after a web view finishes loading a frame.
|
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @param webView The web view has finished loading.
|
|
|
|
*
|
|
|
|
* @return [ Void ]
|
2016-07-25 21:04:20 +08:00
|
|
|
*/
|
|
|
|
- (void) webViewDidFinishLoad:(UIWebView *)webView
|
2016-02-29 20:05:56 +08:00
|
|
|
{
|
|
|
|
UIPrintInteractionController* controller = [self printController];
|
2018-12-21 19:47:34 +08:00
|
|
|
NSString* printerID = _settings[@"printerId"];
|
2016-07-21 03:00:33 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if (![printerID isEqual:[NSNull null]] && printerID.length > 0)
|
|
|
|
{
|
|
|
|
[self sendToPrinter:controller printer:printerID];
|
2016-07-25 21:04:20 +08:00
|
|
|
return;
|
2015-04-23 05:24:33 +08:00
|
|
|
}
|
2016-07-21 03:00:33 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
NSArray* bounds = self.settings[@"bounds"];
|
2016-07-25 21:04:20 +08:00
|
|
|
CGRect rect = [self convertIntoRect:bounds];
|
|
|
|
|
|
|
|
[self presentPrintController:controller fromRect:rect];
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark UIPrintInteractionControllerDelegate
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks the delegate for an object encapsulating the paper size and printing
|
|
|
|
* area to use for the print job. If Paper-Size is given it selects the best
|
|
|
|
* fitting papersize
|
|
|
|
*/
|
|
|
|
- (UIPrintPaper *) printInteractionController:(UIPrintInteractionController *)printInteractionController
|
|
|
|
choosePaper:(NSArray *)paperList
|
|
|
|
{
|
|
|
|
double height = [self.settings[@"paperHeight"] doubleValue];
|
|
|
|
double width = [self.settings[@"paperWidth"] doubleValue];
|
|
|
|
UIPrintPaper* paper;
|
|
|
|
|
|
|
|
if (height && width)
|
|
|
|
{
|
|
|
|
double dotsHeigth = 72 * height / 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
|
|
|
|
paper = [UIPrintPaper bestPaperForPageSize:pageSize
|
|
|
|
withPapersFromArray:paperList];
|
|
|
|
}
|
|
|
|
|
|
|
|
return paper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks the delegate for a length to use when cutting the page. If using roll
|
|
|
|
* printers like Label-Printer (brother QL-710W) you can cut paper after given
|
|
|
|
* length.
|
|
|
|
*/
|
|
|
|
- (CGFloat) printInteractionController:(UIPrintInteractionController *)printInteractionController
|
|
|
|
cutLengthForPaper:(UIPrintPaper *)paper
|
|
|
|
{
|
|
|
|
double length = [self.settings[@"paperCutLength"] doubleValue];
|
|
|
|
CGFloat height = paper.paperSize.height;
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
height = 72 * length / 25.4; //convert milimeters to dots
|
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Core
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks either the printing service is avaible or not.
|
|
|
|
*/
|
|
|
|
- (BOOL) isPrintingAvailable
|
|
|
|
{
|
|
|
|
Class controllerCls = NSClassFromString(@"UIPrintInteractionController");
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
if (!controllerCls) {
|
|
|
|
return NO;
|
2015-04-23 05:24:33 +08:00
|
|
|
}
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
return [self printController] && [UIPrintInteractionController
|
|
|
|
isPrintingAvailable];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 01:58:41 +08:00
|
|
|
/**
|
2018-12-21 19:47:34 +08:00
|
|
|
* Sends the content directly to the specified or previously selected printer.
|
2015-06-24 01:58:41 +08:00
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @param controller The prepared print controller with the content.
|
|
|
|
* @param printer The printer specified by its URL.
|
2016-07-25 21:04:20 +08:00
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @return [ Void ]
|
2016-07-25 21:04:20 +08:00
|
|
|
*/
|
|
|
|
- (void) sendToPrinter:(UIPrintInteractionController*)controller
|
2018-12-21 19:47:34 +08:00
|
|
|
printer:(NSString*)printerID
|
2016-07-25 21:04:20 +08:00
|
|
|
{
|
2018-12-21 19:47:34 +08:00
|
|
|
NSURL* printerURL = [NSURL URLWithString:printerID];
|
|
|
|
|
2017-09-06 02:10:59 +08:00
|
|
|
// check to see if we have previously created this printer to reduce printing/"contacting" time
|
2018-12-21 19:47:34 +08:00
|
|
|
if (!_previousPrinter || ![_previousPrinter.URL.absoluteString isEqualToString:printerID])
|
|
|
|
{
|
|
|
|
_previousPrinter = [UIPrinter printerWithURL:printerURL];
|
2017-09-06 02:10:59 +08:00
|
|
|
}
|
2018-12-21 19:47:34 +08:00
|
|
|
|
|
|
|
[controller printToPrinter:_previousPrinter completionHandler:
|
2016-07-25 21:04:20 +08:00
|
|
|
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
|
2018-12-21 19:47:34 +08:00
|
|
|
CDVPluginResult* result =
|
2016-07-25 21:04:20 +08:00
|
|
|
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
|
messageAsBool:ok];
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
[self.commandDelegate sendPluginResult:result
|
|
|
|
callbackId:self->_callbackId];
|
2016-07-25 21:04:20 +08:00
|
|
|
}];
|
|
|
|
}
|
2016-07-25 20:19:06 +08:00
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
/**
|
2018-12-21 19:47:34 +08:00
|
|
|
* Opens the print controller so that the user can choose between
|
|
|
|
* available iPrinters.
|
|
|
|
*
|
|
|
|
* @param controller The prepared print preview controller.
|
|
|
|
* @param rect The coordinates where to present the preview.
|
|
|
|
*
|
|
|
|
* @return [ Void ]
|
|
|
|
*/
|
|
|
|
- (void) presentPrintController:(UIPrintInteractionController*)controller
|
|
|
|
fromRect:(CGRect)rect
|
|
|
|
{
|
|
|
|
UIPrintInteractionCompletionHandler handler =
|
|
|
|
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
|
|
|
|
CDVPluginResult* result =
|
|
|
|
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
|
messageAsBool:ok];
|
|
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:result
|
|
|
|
callbackId:self.callbackId];
|
|
|
|
};
|
|
|
|
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
[controller presentFromRect:rect
|
|
|
|
inView:self.webView
|
|
|
|
animated:YES
|
|
|
|
completionHandler:handler];
|
|
|
|
} else {
|
|
|
|
[controller presentAnimated:YES
|
|
|
|
completionHandler:handler];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays system interface for selecting a printer.
|
|
|
|
*
|
|
|
|
* @param rect Defines where to display the interface on the screen.
|
2016-07-25 21:04:20 +08:00
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @return [ Void ]
|
2016-07-25 21:04:20 +08:00
|
|
|
*/
|
|
|
|
- (void) presentPrinterPicker:(CGRect)rect
|
|
|
|
{
|
|
|
|
UIPrinterPickerController* controller =
|
|
|
|
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
UIPrinterPickerCompletionHandler handler =
|
|
|
|
^(UIPrinterPickerController *ctrl, BOOL selected, NSError *e) {
|
|
|
|
[self returnPrinterPickerResult:ctrl];
|
|
|
|
};
|
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
2018-12-21 19:47:34 +08:00
|
|
|
[controller presentFromRect:rect
|
|
|
|
inView:self.webView
|
|
|
|
animated:YES
|
|
|
|
completionHandler:handler];
|
|
|
|
} else {
|
|
|
|
[controller presentAnimated:YES
|
|
|
|
completionHandler:handler];
|
2016-07-25 21:04:20 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-25 20:19:06 +08:00
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
/**
|
2018-12-21 19:47:34 +08:00
|
|
|
* Calls the callback funtion with the result of the selected printer.
|
|
|
|
*
|
|
|
|
* @param controller The UIPrinterPickerController used to display
|
|
|
|
* the printer selector interface.
|
2016-07-25 21:04:20 +08:00
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @return [ Void ]
|
2016-07-25 21:04:20 +08:00
|
|
|
*/
|
2018-12-21 19:47:34 +08:00
|
|
|
- (void) returnPrinterPickerResult:(UIPrinterPickerController*)controller
|
2016-07-25 21:04:20 +08:00
|
|
|
{
|
2018-12-21 19:47:34 +08:00
|
|
|
CDVPluginResult* result;
|
|
|
|
UIPrinter* printer = controller.selectedPrinter;
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
[self rememberPrinter:printer];
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if (printer) {
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
|
messageAsString:printer.URL.absoluteString];
|
|
|
|
} else {
|
|
|
|
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
|
2016-07-25 21:04:20 +08:00
|
|
|
}
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
[self.commandDelegate sendPluginResult:result
|
2016-07-25 21:04:20 +08:00
|
|
|
callbackId:_callbackId];
|
2015-06-24 01:58:41 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 21:04:20 +08:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Helper
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
2018-12-21 19:47:34 +08:00
|
|
|
* Returns the shared instance of the printer controller.
|
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
|
|
|
{
|
2018-12-21 19:47:34 +08:00
|
|
|
UIPrintInteractionController* controller = [UIPrintInteractionController
|
|
|
|
sharedPrintController];
|
|
|
|
|
|
|
|
controller.delegate = self;
|
|
|
|
|
|
|
|
return controller;
|
2013-08-13 21:39:22 +08:00
|
|
|
}
|
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.
|
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @param controller The print controller instance.
|
|
|
|
* @param settings The print job specs.
|
2014-09-08 04:52:36 +08:00
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @return 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;
|
2016-07-23 19:48:24 +08:00
|
|
|
UIPrintInfoDuplex duplexMode = UIPrintInfoDuplexNone;
|
2019-01-03 23:52:10 +08:00
|
|
|
long copies = MAX([settings[@"copies"] longValue], 1);
|
2014-09-08 04:52:36 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if ([settings[@"landscape"] boolValue]) {
|
2014-09-08 04:52:36 +08:00
|
|
|
orientation = UIPrintInfoOrientationLandscape;
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if ([settings[@"graystyle"] boolValue]) {
|
2014-09-08 04:52:36 +08:00
|
|
|
outputType = UIPrintInfoOutputGrayscale;
|
|
|
|
}
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
outputType += [settings[@"border"] boolValue] ? 0 : 1;
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if ([settings[@"duplex"] isEqualToString:@"long"]) {
|
2016-07-23 19:48:24 +08:00
|
|
|
duplexMode = UIPrintInfoDuplexLongEdge;
|
|
|
|
} else
|
2018-12-21 19:47:34 +08:00
|
|
|
if ([settings[@"duplex"] isEqualToString:@"short"]) {
|
2016-07-23 19:48:24 +08:00
|
|
|
duplexMode = UIPrintInfoDuplexShortEdge;
|
|
|
|
}
|
|
|
|
|
2019-01-03 23:52:10 +08:00
|
|
|
[printInfo setValue:[NSNumber numberWithLong:copies] forKey:@"_copies"];
|
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
printInfo.outputType = outputType;
|
|
|
|
printInfo.orientation = orientation;
|
2016-07-23 19:48:24 +08:00
|
|
|
printInfo.duplex = duplexMode;
|
2018-12-21 19:47:34 +08:00
|
|
|
printInfo.jobName = settings[@"name"];
|
2016-07-24 19:49:23 +08:00
|
|
|
controller.printInfo = printInfo;
|
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
controller.showsNumberOfCopies = ![settings[@"hideNumberOfCopies"] boolValue];
|
|
|
|
controller.showsPaperSelectionForLoadedPapers = ![settings[@"hidePaperFormat"] boolValue];
|
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
|
|
|
|
2014-09-08 04:52:36 +08:00
|
|
|
/**
|
|
|
|
* Loads the content into the print controller.
|
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @param content The (HTML encoded) content
|
|
|
|
* @param controller The print controller instance
|
|
|
|
*
|
|
|
|
* @return [ Void ]
|
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
|
|
|
{
|
2016-07-29 01:51:10 +08:00
|
|
|
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
|
2018-12-21 19:47:34 +08:00
|
|
|
UIViewPrintFormatter* formatter = self.webView.viewPrintFormatter;
|
|
|
|
bool printSelf = ![content isEqual:[NSNull null]] && content.length == 0;
|
2014-09-08 04:52:36 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if (!printSelf)
|
|
|
|
{
|
2018-12-21 19:13:01 +08:00
|
|
|
UIWebView* page = [[UIWebView alloc] init];
|
2018-12-21 19:47:34 +08:00
|
|
|
formatter = page.viewPrintFormatter;
|
2016-08-03 23:20:10 +08:00
|
|
|
|
2018-12-21 19:13:01 +08:00
|
|
|
page.delegate = self;
|
2014-09-08 04:52:36 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if ([NSURL URLWithString:content])
|
|
|
|
{
|
2018-12-21 19:13:01 +08:00
|
|
|
NSURL *url = [NSURL URLWithString:content];
|
2014-09-12 05:14:18 +08:00
|
|
|
|
2018-12-21 19:13:01 +08:00
|
|
|
[page loadRequest:[NSURLRequest requestWithURL:url]];
|
2018-12-21 19:47:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSString* path = [NSBundle.mainBundle pathForResource:@"www" ofType:nil];
|
|
|
|
NSURL *url = [NSURL fileURLWithPath:path];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
[page loadHTMLString:content baseURL:url];
|
2018-12-21 19:13:01 +08:00
|
|
|
}
|
2014-09-12 05:14:18 +08:00
|
|
|
}
|
2013-08-13 21:39:22 +08:00
|
|
|
|
2018-12-21 19:13:01 +08:00
|
|
|
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
|
2018-12-21 19:47:34 +08:00
|
|
|
[controller setPrintPageRenderer:renderer];
|
2018-12-21 19:13:01 +08:00
|
|
|
|
2018-12-21 19:47:34 +08:00
|
|
|
if (printSelf) {
|
|
|
|
// just trigger the finish load fn straight off if using current webView
|
2018-12-21 19:13:01 +08:00
|
|
|
[self webViewDidFinishLoad:(UIWebView *)self.webView];
|
2018-12-21 19:47:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells the system to pre-select the given printer next time.
|
|
|
|
*
|
|
|
|
* @param printer The printer to remeber.
|
|
|
|
*
|
|
|
|
* @return [ Void ]
|
|
|
|
*/
|
|
|
|
- (void) rememberPrinter:(UIPrinter*)printer
|
|
|
|
{
|
|
|
|
if (!printer) return;
|
|
|
|
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printer];
|
2013-08-13 21:39:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-14 05:31:12 +08:00
|
|
|
/**
|
|
|
|
* Convert Array into Rect object.
|
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @param bounds The bounds
|
2014-10-14 05:31:12 +08:00
|
|
|
*
|
2018-12-21 19:47:34 +08:00
|
|
|
* @return A converted Rect object
|
2014-10-14 05:31:12 +08:00
|
|
|
*/
|
|
|
|
- (CGRect) convertIntoRect:(NSArray*)bounds
|
|
|
|
{
|
2018-12-21 19:47:34 +08:00
|
|
|
return CGRectMake([bounds[0] floatValue],
|
|
|
|
[bounds[1] floatValue],
|
|
|
|
[bounds[2] floatValue],
|
|
|
|
[bounds[3] floatValue]);
|
2014-10-14 05:31:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-11 23:46:31 +08:00
|
|
|
@end
|
2018-12-21 19:47:34 +08:00
|
|
|
|