knyga / html2thermal

JavaScript library to translate subset of HTML into commands which can be executed on the thermal printer.
MIT License
46 stars 3 forks source link

HTML2Thermal GitHub license CircleCI

JavaScript library to translate subset of HTML into commands which would be executed on the thermal printer.

Library is based on the popular node-thermal-printer module which provides adapters for EPSON and STAR thermal printers command line printing.

Motivation

Compare the code in HTML and direct commands for printer.

HTML:

<center>
    <p>dsdasdas</p>
    <div style="font-weight: bold">oneone</div>
</center>

Direct JS commands:

printer.alignCenter()
printer.print('dsdasdas')
printer.newLine()
printer.bold(true)
printer.print('oneone')
printer.bold(true)
printer.newLine()
printer.alignLeft()

HTML code version:

Installation

npm install html2thermal node-thermal-printer

Linux specific

Linux requires build-essentials

sudo apt-get install build-essential

Documentation

Library exports 2 functions:

  1. convert(xml, characterSet = 'SLOVENIA') converts xml to the thermal printer commands.
    • xml as XML code to be converted to commands
    • characterSet
  2. execute(printer, xml, isCut = true) executes the commands on the thermal printer.
    • printer as an instance of the printer from the node-thermal-printer
    • xml as XML code to be executed on the thermal printer
    • isCut defines if cut operation should be executed at the end of the printing process

Supported tags

Supported style properties

If no tag provided, then print operation will be executed.

Character sets

Example

const printer = require('node-thermal-printer')
const {execute} = require('html2thermal')
printer.init({
  type: 'epson',
  // If you connect over TCP
  interface: 'tcp://192.168.192.168',
})

const template = `
<div>hello world</div>
<p>it is</p>
<p>me
me</p>
`

execute(printer, template);

Licence

HTML2Thermal is MIT licenсed.