Ionic native Printer plugin not working

2020-04-21 06:30发布

问题:

(Ionic 2) The plugin located here seems to not work for me on android and ios : http://ionicframework.com/docs/native/printer/

I think that I followed the guidelines from this page, the code is building on both platforms but I got a black screen on ios and a blank one on android when emulate ...

First I started a new project : ionic start PrinterApp --v2

Then I installed platforms : android 6.2.1, ios 4.3.1

Then the two command lines from the plugin page :

ionic plugin add --save de.appplant.cordova.plugin.printer
npm install --save @ionic-native/printer

Then in the home.html I put a line to active the printer :

<button class="button" (click)="print()">Print</button>

And finally my home.ts looks like this :

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();
    let options: PrintOptions = {
         name: 'MyDocument',
         duplex: true,
         landscape: true,
         grayscale: true
       };
    this.printer.print("http://google.com", options);
  }
}

Did anyone have this kind of troubles with the plugin ? Did I do something wrong ? Should I install other things to fix the problem ? And did anyone have a exemple project working well ?

Thank you very much !

回答1:

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



回答2:

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


回答3:

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

npm install --save @ionic-native/printer