Created
December 11, 2018 08:59
-
-
Save AmrMekkawy/b8dfec1d80c705d397bce36e1476f512 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/fromPromise'; | |
import 'rxjs/add/observable/throw'; | |
import 'rxjs/add/operator/map'; | |
declare var qz: any; | |
@Injectable() | |
export class QzTrayService { | |
constructor() {} | |
errorHandler(error: any): Observable<any> { | |
return Observable.throw(error); | |
} | |
// Get list of printers connected | |
getPrinters(): Observable<string[]> { | |
return Observable | |
.fromPromise(qz.websocket.connect().then(() => qz.printers.find())) | |
.map((printers: string[]) => printers) | |
.catch(this.errorHandler); | |
} | |
// Get the SPECIFIC connected printer | |
getPrinter(printerName: string): Observable<string> { | |
return Observable | |
.fromPromise(qz.websocket.connect().then(() => qz.printers.find(printerName))) | |
.map((printer: string) => printer) | |
.catch(this.errorHandler); | |
} | |
// Print data to chosen printer | |
printData(printer: string, data: any): Observable<any> { | |
// Create a default config for the found printer | |
const config = qz.configs.create(printer); | |
return Observable.fromPromise(qz.print(config, data)) | |
.map((anything: any) => anything) | |
.catch(this.errorHandler); | |
} | |
// Disconnect QZ Tray from the browser | |
removePrinter(): void { | |
qz.websocket.disconnect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment