Ionic native Printer plugin not working

梦想与她 提交于 2019-12-02 02:49:25

With the help of Suraj and Gabriel I managed to fix the problem, I needed to go to this page to get informations : http://ionicframework.com/docs/native/#Add_Plugins_to_Your_App_Module

And so typing this line : npm install @ionic-native/core --save

Then into my App.Module.ts adding printer provider like this :

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';

import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

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

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    Printer,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

Thanks again ! Have a great day

//Note: First into your App.Module.ts adding printer provider 
//Then into Printer Page
   import { Component } from '@angular/core';
   import { NavController, NavParams } from 'ionic-angular';
   import { Printer, PrintOptions } from '@ionic-native/printer';


   @Component({
     selector: 'page-printer-view',
     templateUrl: 'printer-view.html'
    })
   export class PrinterViewPage {

   constructor(public navCtrl: NavController, public navParams: 
   NavParams, 
   private printer: Printer) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PrinterViewPage');
  }

  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 !");
  }
}

ionic cordova plugin add --save isiigo-cordova-plugin-printer

npm install --save @ionic-native/printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!