Markdown for receipts. Printable digital receipts. Generate receipt printer commands and images.
npm install receiptlinebash
$ npm install receiptline
`
Usage
receiptline.transform() method transforms ReceiptLine document to printer commands or SVG images.
`javascript
const receiptline = require('receiptline');
const doc = '{code:2012345678903;option:ean,hri}';
// printer example
const printer = {
cpl: 42,
encoding: 'multilingual',
upsideDown: false,
gamma: 1.8,
command: 'escpos'
};
const command = receiptline.transform(doc, printer);
// display example
const display = {
cpl: 42,
encoding: 'multilingual'
};
const svg = receiptline.transform(doc, display);
`
Method
receiptline.transform(doc[, printer])
$3
- doc
- a string of ReceiptLine document
- printer
- an object of printer configuration
$3
- printer commands or SVG images
Printer configuration
- cpl
- characters per line (default: 48)
- encoding
- multilingual: Multilingual (including cp437, cp852, cp858, cp866, cp1252)
- cp437: United States (default)
- cp852: Central European
- cp858: Western European
- cp860: Portuguese
- cp863: French Canadian
- cp865: Nordic
- cp866: Cyrillic
- cp1252: Western European
- cp932: Japanese
- shiftjis: Japanese
- cp936: Simplified Chinese
- gb18030: Simplified Chinese
- cp949: Korean
- ksc5601: Korean
- cp950: Traditional Chinese
- big5: Traditional Chinese
- tis620: Thai
- gradient (for printer)
- false: image processing for text, barcodes, and 2D codes
- true: image processing for photos (default)
- gamma (for printer)
- image gamma correction (range: 0.1 - 10.0, default: 1.8)
- threshold (for printer)
- image thresholding (range: 0 - 255, default: 128)
- upsideDown (for printer)
- false: normal (default)
- true: upside down
- spacing
- false: no line spacing (default)
- true: line spacing
- cutting (for printer)
- false: no paper cutting
- true: paper cutting (default)
- margin (for printer)
- print margin (left) (range: 0 - 24, default: 0)
- marginRight (for printer)
- print margin (right) (range: 0 - 24, default: 0)
- command
- svg: SVG (default)
- text: plain text
- escpos: ESC/POS
- epson: ESC/POS (Epson)
- sii: ESC/POS (Seiko Instruments)
- citizen: ESC/POS (Citizen)
- fit: ESC/POS (Fujitsu)
- impact: ESC/POS (TM-U220)
- impactb: ESC/POS (TM-U220 Font B)
- generic: ESC/POS (Generic) _Experimental_
- starsbcs: StarPRNT (SBCS, Thai)
- starmbcs: StarPRNT (Japanese)
- starmbcs2: StarPRNT (Chinese, Korean)
- starlinesbcs: Star Line Mode (SBCS)
- starlinembcs: Star Line Mode (Japanese)
- starlinembcs2: Star Line Mode (Chinsese, Korean)
- emustarlinesbcs: Command Emulator Star Line Mode (SBCS)
- emustarlinembcs: Command Emulator Star Line Mode (Japanese)
- emustarlinembcs2: Command Emulator Star Line Mode (Chinsese, Korean)
- stargraphic: Star Graphic Mode (TSP100LAN)
- starimpact: Star Mode on dot impact printers _Experimental_
- starimpact2: Star Mode on dot impact printers (Font 5x9 2P-1) _Experimental_
- starimpact3: Star Mode on dot impact printers (Font 5x9 3P-1) _Experimental_
Generate barcodes and QR Codes
receiptline.barcode.generate() method generates a barcode.
receiptline.qrcode.generate() method generates a QR Code.
`javascript
const bar = { data: '1234', type: 'code39', width: 2, quietZone: true };
const barform = receiptline.barcode.generate(bar);
const qr = { data: 'abcdefgh', level: 'm', quietZone: true };
const qrform = receiptline.qrcode.generate(qr);
`
Method
receiptline.barcode.generate(symbol)
$3
- symbol <object>: barcode data
- data <string>: data to encode
- type <string>: barcode type
- upc: UPC-A, UPC-E (check digit can be omitted)
- ean, jan: EAN-13, EAN-8 (check digit can be omitted)
- code39: CODE39
- itf: Interleaved 2 of 5
- codabar, nw7: Codabar (NW-7)
- code93: CODE93
- code128: CODE128
- default: code128
- width <number>: module width
- range: 2-4
- default: 2
- height <number>: module height
- range: 24-240
- default: 72
- hri <boolean>: human readable interpretation
- default: false
- quietZone <boolean>: quiet zone
- default: false
$3
- <object>: barcode form
- length <number>: barcode width (sum of widths)
- height <number>: barcode height
- widths <number[]>: bar / space width (quiet zone, bar, space, ... , bar, quiet zone)
- hri <boolean>: human readable interpretation
- text <string>: human readable interpretation text
Method
receiptline.qrcode.generate(symbol)
$3
- symbol <object>: QR Code data
- data <string>: data to encode
- level <string>: error correction level
- l: Low
- m: Middle
- q: Quartile
- h: High
- default: l
- quietZone <boolean>: quiet zone
- default: false
$3
- <Uint8Array[]>: QR Code form (matrix of dark and light modules)
- 0: light
- 1: dark
QR Code is a registered trademark of DENSO WAVE INCORPORATED.
Transform stream API
receiptline.createTransform() method is the stream version of the receiptline.transform().
`javascript
const fs = require('fs');
const receiptline = require('receiptline');
const source = fs.createReadStream('example.receipt');
const transform = receiptline.createTransform({ command: 'svg' });
const destination = fs.createWriteStream('example.svg');
source.pipe(transform).pipe(destination);
`
Method
receiptline.createTransform([printer])
$3
- printer
- an object of printer configuration
$3
- Transform stream <stream.Transform>
Examples
$3
Display digital receipts in the web browser and print paper receipts on the printer as needed.
$3
Print order slips from cloud server using Epson Server Direct Print or Star CloudPRNT.
$3
Enter receipt markdown text from the web form, transform it to printer commands on the server, and print it out.
$3
Enter receipt markdown text from the web form, transform it to SVG images on the web browser, and display it.
$3
The documents (receipt markdown text) are the same as the examples in the OFSC ReceiptLine Specification.
$3
Customize the command object to output your own commands.
Libraries
$3
It works on both web browser and Node.js.
To output printer commands on a web browser, use Receipt.js or Browserify.
`bash
$ browserify -o receiptline-full.js receiptline.js
`
ReceiptLine Designer
Online version is available.
https://receiptline.github.io/designer/
The ReceiptLine Designer provides more features.
- Edit and preview
- Data transmission via TCP socket
- Hex dump view by listening TCP 19100 port
!Designer
Setup
1. Copy the following files to your working directory
- designer/*
- designer.js
- printers.json
- servers.json
1. Start the server
`bash
$ node designer.js
`
1. Open http://localhost:8080
Use a modern browser.
1. Configure printers.json
`json
"printer_id": {
"host": "127.0.0.1",
"port": 19100,
"asImage": false,
"cpl": 48,
"encoding": "shiftjis",
"gradient": true,
"gamma": 1.8,
"threshold": 128,
"upsideDown": false,
"spacing": true,
"cutting": true,
"command": "escpos"
}
`
- printer_id
- printer identifier (alphanumeric or underscore characters)
- host
- printer address
- port
- printer port (will be 9100)
- asImage
- false: print with device font (default)
- true: print as image (Requires puppeteer or sharp)
- landscape
- false: normal (default)
- true: landscape orientation (for escpos, epson, sii, citizen, starsbcs, starmbcs, starmbcs2)
- resolution
- print resolution for landscape (values: 180, 203, default: 203)
- cpl
- encoding
- gradient
- gamma
- threshold
- upsideDown
- spacing
- cutting
- margin
- marginRight
- see the printer configuration above
- command
- see the printer configuration above
- png: PNG (Requires puppeteer or sharp)
URL query string
The designer can set initial values in the URL query string.
$3
- z: zoom (range: -5-5, default: 0)
- l: language (en, ja, ko, zh-hans, zh-hant, th, ...)
- c: characters per line (range: 24-96, default: 48)
- v: landscape (values: 0, 1, default: 0)
- s: line spacing (values: 0, 1, default: 0)
- p: printer identifier (alphanumeric or underscore characters)
- d: ReceiptLine document
$3
- http://localhost:8080/index.html?c=42&p=tm_series1
- https://receiptline.github.io/designer/index.html?d={code%3A2012345678903%3Boption%3Aean%2Chri}
Serial-LAN Converter
The serial-LAN converter enables test printing to USB / Bluetooth printers that support virtual serial ports.
Setup
1. Install the virtual serial port driver for the printer and Node Serialport
`bash
$ npm install serialport
`
1. Configure servers.json
`json
"serial": {
"host": "127.0.0.1",
"port": 9100,
"device": "COM9"
}
`
- serial
- to enable it, change from _serial
- host
- local address
- port
- local port
- device
- the system path of the serial port
-
Serial port options
`json
"device": "COM9:9600,N,8,1"
`
-
-
- default: 9600,N,8,1,N
- commas can be omitted
-
- 2400, 4800, 9600, 19200, 38400, 57600, 115200
-
- N: none, E: even, O: odd
-
- 8, 7
-
- 1, 2
-
- N: none, R: rts/cts, X: xon/xoff
1. Restart the server
`bash
$ node designer.js
`
Syntax
Railroad diagram
_document_
!document
_line_
!line
_columns_
!columns
_column_
!column
_text_
!text
_char_
!char
_escape_
!escape
_ws (whitespace)_
!ws
_property_
!property
_member_
!member
_key_
!key
_value_
!value
Grammar
Structure
The receipt is made of a table, which separates each column with a pipe |.
|Line|Content|Description|
|---|---|---|
|_column_
| _column_ |
| _column_
_column_ ||Text
Property|Single column|
|_column_ | _column_
| _column_ | _column_ |
| _column_ | _column_
_column_ | _column_ ||Text|Double column|
|_column_ | _..._ | _column_
| _column_ | _..._ | _column_ |
| _column_ | _..._ | _column_
_column_ | _..._ | _column_ ||Text|Multiple columns|
Alignment
The column is attracted to the pipe | like a magnet.
␣ means one or more whitespaces.
|Column|Description|
|---|---|
|_column_
|_column_|
|␣_column_␣||Center|
||_column_
|_column_␣|
_column_␣||Left|
|_column_|
|␣_column_|
|␣_column_|Right|
Text
The text is valid for any column.
`
Asparagus | 0.99
Broccoli | 1.99
Carrot | 2.99
---
^TOTAL | ^5.97
`
Characters are printed in a monospace font (12 x 24 px).
Wide characters are twice as wide as Latin characters (24 x 24 px).
Control characters are ignored.
Special characters in text
Special characters are assigned to characters that are rarely used in the receipt.
|Special character|Description|
|---|---|
|\|Character escape|
|||Column delimiter|
|{|Property delimiter (Start)|
|}|Property delimiter (End)|
|- (1 or more, exclusive)|Horizontal rule|
|= (1 or more, exclusive)|Paper cut|
|~|Space|
|_|Underline|
|"|Emphasis|
| `|Invert|
|^|Double width|
|^^|Double height|
|^^^|2x size|
|^^^^|3x size|
|^^^^^|4x size|
|^^^^^^|5x size|
|^^^^^^^ (7 or more)|6x size|
Escape sequences in text
Escape special characters.
|Escape sequence|Description|
|---|---|
|\\|\|
|\||||
|\{|{|
|\}|}|
|\-|- (Cancel horizontal rule)|
|\=|= (Cancel paper cut)|
|\~|~|
|\_|_|
|\"|_|
|\ `|`|
|\^|^|
|\n|Wrap text manually|
|\x_nn_|Hexadecimal character code|
|\_char_ (Others)|Ignore|
Properties
The property is valid for lines with a single column.
`
{ width: * 10; comment: the column width is specified in characters }
`
|Key|Abbreviation|Value|Case-sensitive|Default|Saved|Description|
|---|---|---|---|---|---|---|
|image|i|_base64 png format_|✓|-|-|Image
(Recommended: monochrome, critical chunks only)|
|code|c|_textdata_|✓|-|-|Barcode / 2D code|
|option|o|_see below_|-|code128 2 72 nohri 3 l|✓|Barcode / 2D code options
(Options are separated by commas or one or more whitespaces)|
|align|a|left
center
right|-|center|✓|Line alignment
(Valid when line width < CPL)|
|width|w|auto
0 -|-|auto
( for all columns)|✓|Column widths (chars)
(Widths are separated by commas or one or more whitespaces)|
|border|b|line
space
none
0 - 2|-|space|✓|Column border (chars)
(Border width: line=1, space=1, none=0)|
|text|t|wrap
nowrap|-|wrap|✓|Text wrapping|
|command|x|_textdata_|✓|-|-|Device-specific commands|
|comment|_|_textdata_|✓|-|-|Comment|
Barcode options
Barcode options are separated by commas or one or more whitespaces.
|Barcode option|Description|
|---|---|
|upc|UPC-A, UPC-E
(Check digit can be omitted)|
|ean
jan|EAN-13, EAN-8
(Check digit can be omitted)|
|code39|CODE39|
|itf|Interleaved 2 of 5|
|codabar
nw7|Codabar (NW-7)|
|code93|CODE93|
|code128|CODE128|
|2 - 4|Barcode module width (px)|
|24 - 240|Barcode module height (px)|
|hri|With human readable interpretation|
|nohri|Without human readable interpretation|
2D code options
2D code options are separated by commas or one or more whitespaces.
|2D code option|Description|
|---|---|
|qrcode|QR Code|
|3 - 8|Cell size (px)|
|l
m
q
h|Error correction level|
Special characters in property values
Special characters in property values are different from special characters in text.
|Special character|Description|
|---|---|
|\|Character escape|
|||Column delimiter|
|{|Property delimiter (Start)|
|}|Property delimiter (End)|
|:|Key-value separator|
|;|Key-value delimiter|
Escape sequences in property values
Escape special characters.
|Escape sequence|Description|
|---|---|
|\\|\|
|\||||
|\{|{|
|\}|}|
|\;|;|
|\n|New line|
|\x_nn_|Hexadecimal character code|
|\`_char_ (Others)|Ignore|