Native "Printer" plugin not working


#1

Hi all,

I have installed the Ionic native Printer plugin in my Ionic 3 app.
I have included it in app.module “providers” array, and Incuded it in page.ts as following:

import { Printer, PrintOptions } from '@ionic-native/printer';

My code is:

this.printer.isAvailable().then(value => {
      console.log("isAvailable", value);

    }).catch(error => {
      // cordova_not_available
      console.log("ERROR");
      console.log(error);
    });

But when I run on a real Android device I get the error:

My Ionic info:

global packages:

@ionic/cli-utils : 1.2.0
Cordova CLI      : 7.0.1 
Ionic CLI        : 3.2.0

local packages:

@ionic/app-scripts              : 1.3.7
@ionic/cli-plugin-cordova       : 1.2.0
@ionic/cli-plugin-ionic-angular : 1.2.0
Cordova Platforms               : android 6.2.3 browser 4.1.0
Ionic Framework                 : ionic-angular 3.2.1

System:

Node       : v7.10.0
OS         : Linux 3.13
Xcode      : not installed
ios-deploy : not installed
ios-sim    : not installed

#2

Did you also install the npm package like this?

npm install --save @ionic-native/printer


#3

Thanks for your answer. Yes, I did.


#4

Somehow it seems to be missing the correct typings… Could you output your package.json over here?


#6

I’m not seeing any printer plugin in there?


#7

You are right, I pasted the wrong version of the package.json.
Here is the actual one:

{
    "name": "dianoia",
    "version": "0.0.1",
    "author": "Ionic Framework",
    "homepage": "http://ionicframework.com/",
    "private": true,
    "scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "lint": "ionic-app-scripts lint",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "4.1.0",
        "@angular/compiler": "4.1.0",
        "@angular/compiler-cli": "4.1.0",
        "@angular/core": "4.1.0",
        "@angular/forms": "4.1.0",
        "@angular/http": "4.1.0",
        "@angular/platform-browser": "4.1.0",
        "@angular/platform-browser-dynamic": "4.1.0",
        "@ionic-native/core": "^3.7.0",
        "@ionic-native/in-app-browser": "^3.10.3",
        "@ionic-native/native-storage": "^3.10.3",
        "@ionic-native/printer": "^3.10.3",
        "@ionic-native/splash-screen": "3.7.0",
        "@ionic-native/status-bar": "3.7.0",
        "@ionic/storage": "2.0.1",
        "cordova-android": "^6.2.3",
        "cordova-browser": "^4.1.0",
        "cordova-plugin-console": "^1.0.5",
        "cordova-plugin-device": "^1.1.4",
        "cordova-plugin-inappbrowser": "^1.7.1",
        "cordova-plugin-nativestorage": "^2.2.2",
        "cordova-plugin-printer": "git+https://github.com/katzer/cordova-plugin-printer.git",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.2",
        "cordova-plugin-whitelist": "^1.3.1",
        "cordova-sqlite-storage": "^2.0.4",
        "ionic-angular": "3.2.1",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionicons": "3.0.0",
        "rxjs": "5.1.1",
        "sw-toolbox": "3.6.0",
        "zone.js": "0.8.10"
    },
    "devDependencies": {
        "@ionic/app-scripts": "1.3.7",
        "@ionic/cli-plugin-cordova": "1.2.0",
        "@ionic/cli-plugin-ionic-angular": "1.2.0",
        "typescript": "2.2.1"
    },
    "description": "An Ionic project",
    "cordova": {
        "plugins": {
            "cordova-plugin-console": {},
            "cordova-plugin-device": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "ionic-plugin-keyboard": {},
            "cordova-plugin-nativestorage": {},
            "cordova-sqlite-storage": {},
            "cordova-plugin-inappbrowser": {},
            "cordova-plugin-printer": {}
        },
        "platforms": [
            "android",
            "browser"
        ]
    }
}

#8

Okay, did you already try to update your ionic-native core package? It’s a few versions behind.


#9

I just updated the ionic-native core package, did npm cache clean, and re-installed all node modules and plugins.
Now the printer is able to run and this.printer.isAvailable() works. But when I do

this.printer.pick().then(result => {
      console.log(result);
    });

I get the following error:

My package.json is the following:

{
    "name": "dianoia",
    "version": "0.0.1",
    "author": "Ionic Framework",
    "homepage": "http://ionicframework.com/",
    "private": true,
    "scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "lint": "ionic-app-scripts lint",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "4.1.0",
        "@angular/compiler": "4.1.0",
        "@angular/compiler-cli": "4.1.0",
        "@angular/core": "4.1.0",
        "@angular/forms": "4.1.0",
        "@angular/http": "4.1.0",
        "@angular/platform-browser": "4.1.0",
        "@angular/platform-browser-dynamic": "4.1.0",
        "@ionic-native/core": "^3.10.3",
        "@ionic-native/in-app-browser": "^3.10.3",
        "@ionic-native/native-storage": "^3.10.3",
        "@ionic-native/printer": "^3.10.3",
        "@ionic-native/splash-screen": "3.7.0",
        "@ionic-native/status-bar": "3.7.0",
        "@ionic/storage": "2.0.1",
        "cordova-android": "^6.2.3",
        "cordova-browser": "^4.1.0",
        "cordova-plugin-console": "^1.0.7",
        "cordova-plugin-device": "^1.1.6",
        "cordova-plugin-inappbrowser": "^1.7.1",
        "cordova-plugin-nativestorage": "^2.2.2",
        "cordova-plugin-printer": "git+https://github.com/katzer/cordova-plugin-printer.git",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.3",
        "cordova-plugin-whitelist": "^1.3.2",
        "cordova-sqlite-storage": "^2.0.4",
        "ionic-angular": "3.2.1",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionicons": "3.0.0",
        "rxjs": "5.1.1",
        "sw-toolbox": "3.6.0",
        "zone.js": "0.8.10"
    },
    "devDependencies": {
        "@ionic/app-scripts": "1.3.7",
        "@ionic/cli-plugin-cordova": "1.2.1",
        "@ionic/cli-plugin-ionic-angular": "1.2.0",
        "@ionic/cli-plugin-ionic1": "1.2.0",
        "typescript": "2.2.1"
    },
    "description": "An Ionic project",
    "cordova": {
        "plugins": {
            "cordova-plugin-console": {},
            "cordova-plugin-device": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "ionic-plugin-keyboard": {},
            "cordova-plugin-nativestorage": {},
            "cordova-sqlite-storage": {},
            "cordova-plugin-inappbrowser": {},
            "cordova-plugin-printer": {}
        },
        "platforms": [
            "android",
            "browser"
        ]
    }
}

#10

Hmm, I found the problem. My code was

this.printer.isAvailable().then(this.printActivityDoc, this.printerNotAvailable);

So in function this.printActivityDoc, the this was undefined.

Thanks for your help :slight_smile:


#11

Glad it’s working now! Scoping is always a point of interest when coming across such issues.


#12

I have two rules of thumb that help me avoid these problems fairly well:

  • never type the word function inside of one, always use arrow functions
  • whenever tempted to pass a method, create a lexically-scoped lambda instead

ERROR TypeError: Cannot read property 'http' of undefined
#13

Hi, I encountered the same issue and I could not get the printer working. May you offer some help?