npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@posprint/command-builder

v0.0.50

Published

JavaScript library that implements the ESC/POS and TSC protocol to buffer.

Downloads

347

Readme

ESC/POS TSC Buffer Builder

Build Status Coverage Status

JavaScript library that implements the ESC/POS and TSC protocol to buffer, also provides an AST like JSON tree structure that you can preparing convertible templates (hmtl/xml) as you want.

Features

  • ESC/POS and TSC protocol compatible
  • Root, text, table, tr, td, img, blank, separator and qrcode nodes include style attribues implementation
  • Command node for cashbox and cut paper implementation
  • Compile semantic nodes to raw buffer according to ESC/POS and TSC protocol

Usage

  1. Installation of nodejs environment
  npm install @posprint/command-builder

The library is distributed as umd package, just import to your front-end project the normally.

  1. Build json tree to command buffer

Build ESC command buffer from json tree

const { buildCommand } = require('@posprint/command-builder')

const jsonTree = {
  "elementName": "root",
  "attributes":{},
  "children": [{
    "elementName": "blank",
    "attributes":{},
    "children": null
  },
  {
    "elementName": "text",
    "attributes": {
      "font-size": "wide-high",
      "align": "center"
    },
    "children": "Change Table"
  }]
}

const cmd = await buildCommand(jsonTree, { type: 'esc', encoding: 'UTF-8', paperSize: [80]})

const buffer =  commands.getBuffer().flush() 

After json tree is built by command bulder, pass buffer to printer tcp socket directly for printing.

Build TSC command buffer from json tree

const { buildCommand } = require('@posprint/command-builder')

{
  "elementName": "root",
  "attributes":{},
  "children": [
  {
    "elementName": "text",
    "attributes":
    {
      "align": "center"
    },
    "children": "---Order List---"
  },
  {
    "elementName": "text",
    "attributes":{},
    "children": "Latte"
  },
  {
    "elementName": "text",
    "attributes":
    {},
    "children": "Take out"
  },
  {
    "elementName": "separator",
    "attributes":
    {},
    "children": null
  },
  {
    "elementName": "text",
    "attributes":
    {},
    "children": "05-18 14:29 #498"
  },
  ]
}

const cmd = await buildCommand(jsonTree, { type: 'tsc', encoding: 'UTF-8', paperSize: [40, 30]})

const buffer =  commands.getBuffer().flush()

Tsc only supports text, separator and root right now, other nodes will be ignored when you printer type is TSC. We are extending more tsc nodes, for example table, img, blank.

  1. Send raw buffer to printer through tcp connection

View example folder to get demo and screenshot how to buffer be printed by physical printer.

API

buildCommand

buildCommand(json, option)
  • json: json tree structure of nodes, view node tree description below
  • object.type: string type allows 'esc' and 'tsc', default value is 'esc'
  • object.encoding: string type of encoding, default value is 'UTF-8'
  • object.paperSize: array type of printer paper size, 'tsc' type contains two elements, default value is [80]. ie, [80], [58], [40, 30]

Returns a Template instance which can invoke getBuffer() method to get raw buffer.

validateNode

validateNode(json, option)
  • json, option parameters are the same as buildCommand, the difference is json tree not be built buffer in order to validate json tree of nodes

Node Json Tree

Json tree that contains multiple nodes must have a root node, each node contains elementName, attributes, children and other attributes.

The specific json tree format is as follows:

  {
    "elementName": "root",
    "attributes": {},
    "children": [{
        "elementName": "blank",
        "attributes": {},
        "children": null
      },
      {
        "elementName": "text",
        "attributes":
        {
          "align": "center"
        },
        "children": "Order List"
      }
    ]
  }

node specification

| Attribute | Type | Requirement | specification | |-----|----|---|----| | elementName | string | required | A node describes a type of printing instruction, text, picture, QR code, etc. Allowed element name see supported nodes below. | | attributes | object | optional | Describe the style of the node, font, style, alignment, etc. | | children | array, string, null | optional | The content printed by printer or the child node. |

You can use different json parser to convert the HTML/XML or other source file into a json tree.

Supported Nodes

  • root
  • text
  • command
  • blank
  • img
  • qrcode
  • separator
  • table
  • tr
  • td
  • section

Exmaples

View source code in the exmaples folder about ESC and TSC content printing.

Actual print effect 1 Actual print effect 2

Recommended HTML/XML Parser

  1. Live demo of parsers look here
  2. React temple parser supports all nodes of current library is available here