JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 41
  • Score
    100M100P100Q62119F
  • License MIT

A ESC/POS library for node.js

Package Exports

  • npos

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (npos) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npos NPM version Build Status Dependency Status Coverage percentage

A ESC/POS library for node

Installation

$ npm install --save npos

Parser

Parse ESC/POS binary buffer.

parse

var npos = require('npos');
var parser = npos.parser();
var buffer = fs.readFileSync('sample.bin');

parser.parse(buffer).then(function (ast) {
  console.log(ast);
});

The ast form parse is like:

{
  raw: <Buffer ...>,
  error: <error>,
  tree: [
    {
      code: 27,         // command code in decimal 
      hex: '1B',        // command code in hex
      ascii: 'ESC',     // command code in ascii name
      offset: 0,        // data offset from buffer
      length: 2,        // data length 
      fn: '@',          // command fn charater
    },
    ....
  ],
  entries: [            // decoded entries
    {
      type: 'raster',
      nodes: [          // nodes handled by codec from ast
      ...
      ],
      data: <Object>    // data decoded by codec
    },
    ...
  ]
}

rules

rules is used to parse esc/pos binary to ast tree.

Example rules:

var npos = require('npos');
var rules = {};

rules[npos.ESC] = {     // 'ESC' commands
  '!': 1,               // 'ESC !' command. Swallow one parameter byte
  '$': 2,           
  '%': 1,
  '&': [0, ycc],        // 'ESC &' command. Using 'ycc' function to decode from offset 0
  '*': [0, 'bitimage'], // 'ESC *' command. Using builtin decoder to decode from offset 0.
  '-': 1,
  '2': 0,
  '3': 1,
  '=': 1,
  '?': 1,
  '@': 0,
  'D': [0, 'escd'],
  'E': 1,
  'G': 1,
  'J': 1,
  'L': 0,
  'M': 1,
  'R': 1,
  'S': 0,
  'T': 1,
  'V': 1,
  'W': 8,
  '\\': 2,
  'a': 1,
  'c': 2,
  'd': 1,
  'p': 3,
  'r': 0, // Select print color
  't': 1,
  '{': 1,
  'B': 2, // Unknown Command
  'Z': [3, 'd16']
};

// Custom splitter with parameter buffer and offset specified with rule.
function ycc(buf, offset) {
  if (buf.length - offset < 3) {
    return buf.length;
  }
  var k = buf[offset + 2] - buf[offset + 1] + 1;
  var num = 3;
  for (var i = 0; i < k; i++) {
    num += buf[offset + num] * buf[offset] + 1;
  }
  return num;
};

or extend the builtin rules:

var npos = require('npos');
npos.rules[npos.ESC]['&'] = [0, ycc];

Here are builtin rules and builtin splitters.

codec

npos use codec to decode from ast tree parsed from binary.

Here are builtin codecs

We can extend codecs as need. For example:

// sampleCodec.js

// commands supported
exports.commands = ['GSv0'];   
// decode from raw data and nodes
exports.decode = function (raw, nodes) {  
  // traverse nodes to decode and return result decoded
}
var npos = require('npos');
npos.codecs['sampleCodec'] = require('./sampleCodec');

to pdf

See: example

License

© taoyuan