Native "Printer" plugin not working

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:

image

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

Did you also install the npm package like this?

npm install --save @ionic-native/printer

Thanks for your answer. Yes, I did.

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

I’m not seeing any printer plugin in there?

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"
        ]
    }
}

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

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:
image

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"
        ]
    }
}

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:

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

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
2 Likes

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

i have a problem with printer


my code :

print(){
this.printer.isAvailable().then(this.onSuccessLoad, this.onErrorLoad);

}

onSuccessLoad(){
let options: PrintOptions = {
name: ‘MyDocument’,
printerId: ‘Brother HL-L2360D series Printer’,
duplex: true,
landscape: true,
grayscale: true
};

this.printer.print("http://google.com",options).then(this.onSuccessPrint, 
this.onErrorPrint); 

}

onErrorLoad(){
alert('Error : printing is unavailable on your device ');
}
can you help me .

Is there any way to use url links as content for the printer plugin.I mean to say For example if i want to use “https://www.google.com/” as content.How i am suppose to use it? I got struck in this issue from 2 days. Any suggestions accepted. Thanks in advance.

i’m encountering with same issue. But in my case Printer Plugin was not installed correctly.
and also in android.manifest.xml add android:usesCleartextTraffic=“true” in tag

yes,
please check first your android version with command ionic info. in my case version is 9