Search code examples
iosphonegap-pluginscordova-2.0.0

Phonegap IOS PrintPlugin for Cordova 2.0


hi iam working in phonegap IOS (cordova 2.0) i need a print Plugin and i found only one plugin that not supporting 2.0 - link only support up to cordova 1.5. anybody please help me , is there any PrintPlugin that support cordova 2.0.

Can anyone help ? ?


Solution

  • As of this writing, the PrintPlugin from the GitHub repository is targeted at Cordova 1.5. If you get that version, and make the following changes, you can compile within a Cordova 2.0 Project.

    NOTICE: I have seen a bug in this entire plugin which is an issue with printing more than one page. I have found that it is related to a lack of logic to wait for the webView to complete load before printing. I am currently working a a new version of this plugin to allow for custom headers, page numbering etc. But for now..... ;)

    In the current repository .m file, it has the following function declaration:

    - (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
        NSUInteger argc = [arguments count];
    
        if (argc < 1) {
            return; 
        }
        self.printHTML = [arguments objectAtIndex:0];
    
      ... << snipped />> ... 
    
    
        [self doPrint];
    }
    

    This is followed with a separate doPrint function.

    - (void) doPrint{
        if (![self isPrintServiceAvailable]){
            [self callbackWithFuntion:self.failCallback withData: @"{success: false, available: false}"];
    
            return;
        }
    
        UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    
          if (!controller){
              return;
          }
    
            ... << snipped  (cut and paste to below) />> ...
    }
    

    I updated my local copy and merged the two into a single function, and utilized a different mechanism for obtaining the arguments. Following is my .m function declaration:

    - (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
    {   
        // NSUInteger argc = [arguments count];
    
        NSString* printHTML = [options valueForKey:@"printHTML"];
        NSString* successCallback = [options valueForKey:@"success"];
        NSString* failCallback = [options valueForKey:@"fail"];
        NSString* dialogLeftPos = [options valueForKey:@"dialogLeftPos"];
        NSString* dialogTopPos = [options valueForKey:@"dialogTopPos"];
    
    
      UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    
      if (!controller){
          return;
      }
    
        ... <<  snipped (paste the rest from your original block here />> ...
    }
    

    I then also modified the .JS to create an object instead of simply the parameter list.

    The current repository PrintPlugin.js has:

    return Cordova.exec("PrintPlugin.print", printHTML, callbackPrefix + '.success', callbackPrefix + '.fail', dialogLeftPos, dialogTopPos);
    

    return Cordova.exec("PrintPlugin.print", printHTML, callbackPrefix + '.success', callbackPrefix + '.fail', dialogLeftPos, dialogTopPos);

    I changed this to the following:

    var args = {
        'printHTML': printHTML,
        'success': callbackPrefix + '.success',
        'fail': callbackPrefix + '.fail', 
        'dialogLeftPos': dialogLeftPos,
        'dialogTopPos': dialogTopPos
    };
    
    cordova.exec(null, null, "PrintPlugin", "print", [args]);
    

    Now, it may turn out that I could have simply changed the case in the Cordova to cordova, but that is pure hindsight. Either way, I hope this helps!