GithubHelp home page GithubHelp logo

safalshrestha / nativescript-star-printer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eddyverbruggen/nativescript-star-printer

0.0 2.0 0.0 28.6 MB

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

License: MIT License

Shell 1.05% Objective-C 60.75% C 18.20% TypeScript 20.00%

nativescript-star-printer's Introduction

NativeScript Star Printer

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 most useful property on the SPPrinter class is the portName which you will need in other API methods.

The only other property is modelName.

connect

Once you know the printer port name, you can connect to it.

Note that there's no need to connect if you want to print as the print function does this automatically.

this.starPrinter.connect({
  portName: thePortName
}).then((result: SPPrinterStatusResult) => console.log("Connected: " + result.connected));

getPrinterStatus

After connecting to a printer, you can use this method to poll for the 'online' and 'paper' statuses.

this.starPrinter.getPrinterStatus({
  portName: this.lastConnectedPrinterPort
}).then(result => {
  const online: boolean = result.online;
  const onlineStatus: PrinterOnlineStatus = result.onlineStatus;
  const paperStatus: PrinterPaperStatus = result.paperStatus;
});

print

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

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

So what are those commands? Let's recreate the fake receipt below to answer that (see the TypeScript definition for all options):

const image = ImageSource.fromFile("~/res/mww-logo.png");

// Note that a standard 3 inch roll is 48 characters wide - we use that knowledge for our "columns"
let commands = new SPCommands()
    .image(
        image,
        true, // diffuse
        true // align center (set to 'false' to align left)
     )
    .alignCenter()
    .text("My Awesome Boutique").newLine()
    .text("In a shop near you").newLine()
    .setFont("smaller")
    .text("Planet Earth").newLine()
    .setFont("default")
    .newLine()
    .text("Date: 11/11/2017                   Time: 3:15 PM")
    .horizontalLine()
    .newLine()
    .textBold("SKU           Description                  Total").newLine()
    .text("300678566     Plain White Tee             €10.99").newLine()
    .text("300692003     Black Dénim                 €29.99").newLine()
    .text("300651148     Blue Denim                  €29.99").newLine()
    .newLine()
    .newLine()
    .barcode({
      type: "Code128",
      value: "12345678",
      width: "large",
      height: 60,
      appendEncodedValue: false
    })
    .newLine()
    .cutPaper();

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

openCashDrawer

In case 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.

nativescript-star-printer's People

Contributors

eddyverbruggen avatar

Watchers

James Cloos avatar Safal Shrestha avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.