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.
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:
- allows to create preview of what will be printed in the browser
- makes code more readable
- reduces integration time
npm install html2thermal node-thermal-printerLinux requires build-essentials
sudo apt-get install build-essentialLibrary exports 2 functions:
convert(xml, characterSet = 'SLOVENIA')convertsxmlto the thermal printer commands.xmlas XML code to be converted to commandscharacterSet
execute(printer, xml, isCut = true)executes the commands on the thermal printer.printeras an instance of the printer from the node-thermal-printerxmlas XML code to be executed on the thermal printerisCutdefines if cut operation should be executed at the end of the printing process
<beep/>perform sound of an internal beeper if present<br/>breaks the line (new line)<b>...</b>sets text bold<center>...</center>aligns text to center<left>...</left>aligns text to left<right>...</right>aligns text to right<cut />cuts the paper if printer supports it<partialcut />cuts the paper and leaves the bridge in the middle if printer supports it<div>...</div>isolates content on the separate line<p>...</p>isolates content on the separate line<doubleheight>...</doubleheight>sets text to double height<u>...</u>sets underline to the text<ud>...</ud>sets double underline or thick to the text<doublewidth>...</doublewidth>sets text to double width<fonta>...</fonta>changes text font to the first supported (A or default)<fontb>...</fontb>changes text font to the second supported (B)<hr />draws the line-separator<img src="..." height="..." width="..." />prints the image from src (file:,http:,https:,data:, ...), width and height in pixels are optional<invert>...</invert>inverts background and foreground colors<normal>...</normal>resets all text settings to normal, redundant operation<opencashdrawer />opens cash drawer if present<code128 data="..." />prints code128 bar code with data provided in the data property<qrcode data="..." />prints QR code with data provided in the data property<quadarea>...</quadarea>sets text to quad area<rotate180>...</rotate180>prints content upside down<table>...</table>table<tr>...</tr>table row<td width="..." align="..." bold="..." style="...">...<td>cell of the table row,widthproperty is optional and in factor (0.5,0.25, ...),alignproperty is optional (left,right,center),boldproperty is optional (true),styleproperty is optional
font-style: boldsets text bold (NOTE: no support for number value yet)
If no tag provided, then print operation will be executed.
PC437_USAPC850_MULTILINGUALPC860_PORTUGUESEƒPC863_CANADIAN_FRENCHPC865_NORDICPC851_GREEKPC857_TURKISHPC737_GREEKISO8859_7_GREEKWPC1252PC866_CYRILLIC2PC852_LATIN2SLOVENIAPC858_EUROWPC775_BALTIC_RIMPC855_CYRILLICPC861_ICELANDICPC862_HEBREWPC864_ARABICPC869_GREEKISO8859_2_LATIN2ISO8859_15_LATIN9PC1125_UKRANIANWPC1250_LATIN2WPC1251_CYRILLICWPC1253_GREEKWPC1254_TURKISHWPC1255_HEBREWWPC1256_ARABICWPC1257_BALTIC_RIMWPC1258_VIETNAMESEKZ1048_KAZAKHSTAN
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);HTML2Thermal is MIT licenсed.