1.0.3 • Published 4 years ago

cordova-plugin-ia-bixolon v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

cordova-plugin-ia-bixolon

A cordova plugin to utilize bluetooth printers that use the ESC POS system. This plugin opens a serial connection with the printer.

NOTE: This repository is written and maintained with personal use in mind. As such, I will not be taking on issues, updates, requests and such. If you are looking for a regularly maintained plugin, look elsewhere. :-|

ESC POS Commands

Commands can differ among manufacturers. Be sure to check with your manufacturer and adapt the commands accordingly. Some examples:

Installation

ionic cordova plugin add cordova-plugin-ia-bixolon

Usage - ANDROID

The android-side of the plugin gets exposed globally as BTPrinter. To appease tha language server and lint, declare the variable at the top of the file as so:

declare var BTPrinter: any;

I am planning on creating an Ionic Native wrapper to convert to Promise-based usage. For now, check out the interface at the bottom of the readme if you'd like.

Supported Functions

  • connect - string, successFunction, errorFunction
  • cutPaper - successFunction, errorFunction
  • disconnect - successFunction, errorFunction
  • list - successFunction, errorFunction
  • status - successFunction, errorFunction
  • setBrand - string, successFunction, errorFunction
  • printBase64 - string, align, successFunction, errorFunction
  • printImageUrl - string, align, successFunction, errorFunction
  • printPOSCommand - string, successFunction, errorFunction
  • printText - string, successFunction, errorFunction

connect

Attempt to open a serial connection with the printer. It is best to use the list command, and then send the name you received to this command.

let printerName = 'printer123';

BTPrinter.connect(printerName,
    function(){
        console.log('connected');
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);

disconnect

BTPrinter.disconnect(
    function(){
        console.log('disconnected');
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);

list

BTPrinter.list(
    function(data){
        console.log(data);
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);
data = [ 'name1', 'address1', typeNumber1, 'name2', 'address2', typeNumber2, ... ]

printBase64

let image64 = 'data:image/png;base64,iVBORw0KGgoAAAAN...   ...xDts=';
let align = '0'; // See Align Options

BTPrinter.printBase64(image64, align,
    function(){
        console.log('image print command sent to printer');
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);

printImageUrl

Example

let imageUrl = '/storage/emulated/0/Pictures/myfolder/myimage.jpg'; // Maximum Size: 300x300px
let align = '0'; // See Align Options

BTPrinter.printImageUrl(imageUrl, align,
    function(){
        console.log('image print command sent to printer');
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);

printPOSCommand

Take note: Commands can differ from one brand of POS printer to another.

Example

let commandString = '\x1D\x28\x41'; // Command to execute test print on SAM4S printers
BTPrinter.printPOSCommand(commandString,
    function(){
        console.log('image print command sent to printer');
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);

printText

Example

let text = 'This is a line of text.';
BTPrinter.printText(text,
    function(){
        console.log('text sent to printer');
    },
    function(errorMessage){
        console.error(errorMessage);
    }
);

status

Example

// TODO Expand code to include all posibile variables
// TODO Return object in place of string array
// TODO Example

Returns

// TODO Example

Option Values

Size options

     0 = CHAR_SIZE_01 // equivalent 0x1B, 0x21, 0x00
     8 = CHAR_SIZE_08 // equivalent 0x1B, 0x21, 0x08
    10 = CHAR_SIZE_10 // equivalent 0x1B, 0x21, 0x10
    11 = CHAR_SIZE_11 // equivalent 0x1B, 0x21, 0x11
    20 = CHAR_SIZE_20 // equivalent 0x1B, 0x21, 0x20
    30 = CHAR_SIZE_30 // equivalent 0x1B, 0x21, 0x30
    31 = CHAR_SIZE_31 // equivalent 0x1B, 0x21, 0x31
    51 = CHAR_SIZE_51 // equivalent 0x1B, 0x21, 0x51
    61 = CHAR_SIZE_61 // equivalent 0x1B, 0x21, 0x61

Align options

    0 = ESC_ALIGN_LEFT // equivalent 0x1B, 0x61, 0x00
    1 = ESC_ALIGN_CENTER // equivalent 0x1B, 0x61, 0x01
    2 = ESC_ALIGN_RIGHT // equivalent 0x1B, 0x61, 0x02

plugin-interface

declare var BTPrinter: {
  connect(printerName: string, fnSuccess: any, fnError: any): any;
  disconnect(fnSuccess: any, fnError: any): any;
  list(fnSuccess: any, fnError: any): any;
  status(fnSuccess: any, fnError: any): any;
  cutPaper(fnSuccess: any, fnError: any): any;
  setBrand(brandName: string, fnSuccess: any, fnError: any): any;

  printBase64(image64: string, align: any, fnSuccess: any, fnError: any): any;
  printImageUrl(imageUrl: string, align: any, fnSuccess: any, fnError: any): any;
  printPOSCommand(commandString: string, fnSuccess: any, fnError: any): any;
  printText(text: string, fnSuccess: any, fnError: any): any;
};

Usage - IOS

The plugin creates the 'global' object BixolonPrint. As with all other native plugins, the variable is only accessible once the platform is ready.

Add this to the top of your service outside the component declaration:

declare var BixolonPrint: {
    discovery(fnSuccess: any, fnError: any): void;
    connect(portName: string, fnSuccess: any, fnError: any): void;
    disconnect(fnSuccess: any, fnError: any): void;
    cutPaper(fnSuccess: any, fnError: any): void;
    getStatus(printStatus: boolean, fnSuccess: any, fnError: any): void;
    printText(lines: string[], fnSuccess: any, fnError: any): void;
    printImage64(imageBase64: string, fnSuccess: any, fnError: any): void;
    printReceipt(imageBase64: string, lines: string[], fnSuccess: any, fnError: any): void;
};

Functions

discovery

BixolonPrint.discovery(
    (printers: any[]) => {
        // Array of printers to use here
    },
    (error: string) => {
        // Error handling here
    }
);

connect

// Best to use the modelName you get from `discovery` here.
const printerModelName = '_____';

BixolonPrint.connect(printerModelName,
    (response: any) => {
        // Yay, connected. You can now do getStatus().
    },
    (error: string) => {
        // Error handling here
    }
);

disconnect

BixolonPrint.disconnect(
    (response: any) => {
        // Now disconnected
    },
    (error: string) => {
        // Error handling here
    }
);

cutPaper

BixolonPrint.cutPaper(
    (response: any) => {
        // Cut command was successful
    },
    (error: string) => {
        // Error handling here
    }
);

getStatus

BixolonPrint.getStatus(false,
    (status: any) => {
        // This part is still TODO, currently status is always null
    },
    (error: string) => {
        // Error handling here
    }
);

printText

// See BixolonCommands -> bottom of readme
const lines = ['Line 1', 'Line 2', BixolonCommands.FONT_BOLD, 'Bold Line 3'];
BixolonPrint.printText(lines,
    (response: any) => {
        // yay
    },
    (error: string) => {
        // nay handling here
    }
);

printImage64

// On iOS, the data length must be a multiple of 4, so pad with '=' as needed.
const imageBase64 = 'UT/8xQ+AkmQI_________fHl7yvfrsexnr==';

BixolonPrint.printImage64(imageBase64,
    (response: any) => {
        // yay
    },
    (error: string) => {
        // nay handling here
    }
);

printReceipt

Essentialy a combination of printText and printImage. However, when imageBase64 is empty, no image is printed.

const imageBase64 = 'UT/8xQ+AkmQI_________fHl7yvfrsexnr==';
const lines = ['Line 1', 'Line 2', BixolonCommands.FONT_BOLD, 'Bold Line 3'];

BixolonPrint.printReceipt(imageBase64, lines,
    (response: any) => {
        // yay
    },
    (error: string) => {
        // nay handling here
    }
);

ESC Commands

// Add these as separate lines amongst your own
enum BixolonCommands {
    FONT_A = '[FONTA]',
    FONT_B = '[FONTB]',
    FONT_C = '[FONTC]',
    ALIGN_LEFT = '[LEFT]',
    ALIGN_CENTER = '[CENTER]',
    ALIGN_RIGHT = '[RIGHT]',
    FONT_BOLD = '[BOLD]',
    FONT_NORMAL = '[NORMAL]',
    TEXT_SMALL = '[SMALL]',
    TEXT_MEDIUM = '[MEDIUM]'
}