slolam / nativescript-star-printer

:star2: Print directly to Star Micronics printers from your NativeScript app! http://www.starmicronics.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NativeScript Star Printer

Build Status NPM version Downloads Twitter Follow

That's the demo app in action, printing on a Star Micronics TSP650II

Installation

tns plugin add nativescript-star-printer

API

requiring / importing the plugin

All examples below assume you're using TypeScript, but here's how to require the plugin with plain old JS as well:

JavaScript

var StarPrinterPlugin = require("nativescript-star-printer");
var starPrinter = new StarPrinterPlugin.StarPrinter();

TypeScript

import { StarPrinter, SPPrinter, SPCommands } from "nativescript-star-printer";

export Class MyPrintingClass {
  private starPrinter: StarPrinter;
  
  constructor() {
    this.starPrinter = new StarPrinter();
  }
}

searchPrinters

If you're searching for a Bluetooth printer, enable Bluetooth in the device settings and pair/connect the printer. Then do:

this.starPrinter.searchPrinters().then(
    (printers: Array<SPPrinter>) => {
      console.log(`Found ${printers.length} printers`);
    }, (err: string) => {
      console.log(`Search printers error: ${err}`);
    });

The only useful property on the SPPrinter class is the portName which you will need in other API methods.

print

Once you've got the port of the printer you want to print on, just do:

this.starPrinter.print({
  portName: this.selectedPrinterPort,
  commands: commands
});

So what are those commands? Let's recreate the receipt below to answer that:

// Note that a standard 3 inch roll is 48 characters wide - we use that knowledge for our "columns"
let commands = new SPCommands()
    .alignCenter() // designates the start of center-aligned text. Use alignLeft() to.. guess what :)
    .text(     "My Awesome Boutique").newLine()
    .text(     "In a shop near you").newLine()
    .text(     "Planet Earth").newLine()
    .newLine() // this will draw a blank line
    .text(     "Date: 11/11/2017                   Time: 3:15 PM")
    .horizontalLine() // Note that horizontal lines include newLine() commands as well
    .newLine() // blank line
    .textBold( "SKU           Description                  Total").newLine()
    .text(     "300678566     Plain White Tee              10.99").newLine()
    .text(     "300692003     Black Denim                  29.99").newLine()
    .text(     "300651148     Blue Denim                   29.99").newLine()
    .newLine() // blank line
    .text(     "Subtotal                                   70.97").newLine()
    .text(     "VAT                                         4.03")
    .horizontalLine()
    .text(     "Total                                 ")
    .textLarge("75.00").newLine() // Note that large text takes up double the space
    .newLine()
    .cutPaper(); // this makes the receipt much easier to tear off :)

this.starPrinter.print({
  portName: this.selectedPrinterPort,
  commands: commands
});

openCashDrawer

When a cash drawer is connected via the UTP (network) connector of the Star printer, you can open the drawer from your code!

this.starPrinter.openCashDrawer({
  portName: this.selectedPrinterPort
});

iOS runtime permission reason

iOS 10+ requires a permission popup when connecting (the first) time to a Bluetooth peripheral explaining why it needs to connect.

You can provide your own reason by adding something like this to app/App_Resources/ios/Info.plist:

  <key>NSBluetoothPeripheralUsageDescription</key>
  <string>My reason justifying fooling around with your Bluetooth</string>

To not crash your app in case you forgot to provide the reason this plugin adds an empty reason to the .plist during build. This value gets overridden by anything you specified yourself. You're welcome.

Known limitations

On iOS you want to run this on a real device.

Future work

Possibly add more print formatting options.

About

:star2: Print directly to Star Micronics printers from your NativeScript app! http://www.starmicronics.com/

License:MIT License


Languages

Language:Objective-C 60.2%Language:C 22.1%Language:TypeScript 16.4%Language:Shell 1.3%