Ionic printer issue


#1

Hi can any one help me please. I have issue related to ionic printer plugin.

so my home.html is

<ion-content padding>
 <button (click)="print()">Print</button>
</ion-content>

and my home.ts is

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Printer, PrintOptions } from '@ionic-native/printer';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController,private printer: Printer) {
  }
print()
{
this.printer.isAvailable().then(this.onSuccessLoad, this.onErrorLoad);
}

onSuccessLoad(){
let options: PrintOptions = {
name: 'MyDocument',
printerId: 'My Printer XYZ',
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 ');
}

onSuccessPrint(){
alert("printing done successfully !");
}

onErrorPrint(){
alert("Error while printing !");
}

}

i have added printer in provider also in app.module.ts

so when app get loaded I get print button so when i click on it call print() method.
in print method i have
this.printer.isAvailable().then(this.onSuccessLoad, this.onErrorLoad);
so
it calls this.onErrorLoad method.It means printer is not available.
my onErrorLoad method is

onErrorLoad(){
alert('Error : printing is unavailable on your device ');
}

i want it should call this.onSuccessLoad.
please help me to make printer available so that this.onSuccessLoad can run.

Thanks


#6

What platform and device are you testing on?
What is your ionic info output?
Are you sure the platform is ready when you click the button?


#7

Hi Sujan
Thanks for your help i was running app on emulator. But when i run the command ionic cordova run android then it worked fine.

Thank you very much.