Ionic Printer


#1

I used Printer plugin from ionicframework-ionic native - printer plugin but it does not show even the print preview, i follow all the steps.
1.ionic cordova plugin add de.appplant.cordova.plugin.printer
2. npm install --save @ionic-native/printer
3. Add this plugin to your app’s module

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

constructor(private printer: Printer) { }

printMe(){

this.printer.isAvailable().then(onSuccess, onError);

let options: PrintOptions = {
name: ‘MyDocument’,
printerId: ‘printer007’,
duplex: true,
landscape: true,
grayscale: true
};

this.printer.print(content, options).then(onSuccess, onError);
}

even the print preview it does not show, please i need ur help…


#2

Same to me, did you find an answer?? :C


#3

not yet Aleehpandita,


#4

//try this and let me know if it works…
import { Component } from ‘@angular/core’;
import { NavController, Platform } from ‘ionic-angular’;

import pdfMake from ‘pdfmake/build/pdfmake’;
import pdfFonts from ‘pdfmake/build/vfs_fonts’;
pdfMake.vfs = pdfFonts.pdfMake.vfs;

import { File } from ‘@ionic-native/file’;
import { FileOpener } from ‘@ionic-native/file-opener’;
import {Printer, PrintOptions} from ‘@ionic-native/printer’;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {
letterObj = {
to: ‘’,
from: ‘’,
text: ‘’
}

pdfObj = null;

constructor(public navCtrl: NavController,private printer: Printer, private plt: Platform, private file: File, private fileOpener: FileOpener) { }
//----------------------create pdf-----------------------------------
createPdf() {
var docDefinition = {
content: [
{ text: ‘REMINDER’, style: ‘header’ },
{ text: new Date().toTimeString(), alignment: ‘right’ },

    { text: 'From', style: 'subheader' },
    { text: this.letterObj.from },

    { text: 'To', style: 'subheader' },
    this.letterObj.to,

    { text: this.letterObj.text, style: 'story', margin: [0, 20, 0, 20] },

    {
      ul: [
        'udara',
        'lakshan',
        'attanayake',
      ]
    }
  ],
  styles: {
    header: {
      fontSize: 18,
      bold: true,
    },
    subheader: {
      fontSize: 14,
      bold: true,
      margin: [0, 15, 0, 0]
    },
    story: {
      italic: true,
      alignment: 'center',
      width: '50%',
    }
  }
}
this.pdfObj = pdfMake.createPdf(docDefinition);

//----------------------download-----------------------------------
if (this.plt.is(‘cordova’)) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: ‘application/pdf’ });

      // Save the PDF to the data Directory of our App
      this.file.writeFile(this.file.dataDirectory, 'myletter.pdf', blob, { replace: true }).then(fileEntry => {
        // Open the PDf with the correct OS tools
      this.fileOpener.open(this.file.dataDirectory + 'myletter.pdf', 'application/pdf');
      })
    });
  } else {
    // On a browser simply use download!
    this.pdfObj.download();
  }

//----------------------print-----------------------------------
this.printer.isAvailable().then(function(){
this.printer.print(this.pdfObj).then(function(){
alert(“printing done successfully !”);
},
function(){
alert(“Error while printing !”);
});
},
function(){
alert('Error : printing is unavailable on your device ');
});

}