Print libraries for receipt printers, simple and easy with markdown, printer status support.
const markdown = `^^^RECEIPT
03/18/2024, 12:34:56 PM
Asparagus | 1| 1.00
Broccoli | 2| 2.00
Carrot | 3| 3.00
---
^TOTAL | ^6.00`;
const receipt = Receipt.from(markdown, '-c 42 -l en');
const png = await receipt.toPNG();
Receipt.js is simple print libraries for receipt printers that prints with easy markdown data for receipts and returns printer status. Even without a printer, it can output images.
A development tool is provided to edit, preview, and print the receipt markdown.
https://receiptline.github.io/receiptjs-designer/
The details of the receipt markdown are explained at
https://github.com/receiptline/receiptline
The following files are required to use the Receipt API.
<script type="text/javascript" src="receipt.js"></script>
<script type="text/javascript" src="qrcode-generator/qrcode.js"></script>
const markdown = `^^^RECEIPT
03/18/2024, 12:34:56 PM
Asparagus | 1| 1.00
Broccoli | 2| 2.00
Carrot | 3| 3.00
---
^TOTAL | ^6.00`;
const receipt = Receipt.from(markdown, '-c 42 -l en');
const png = await receipt.toPNG();
The Receipt.from() static method creates a new Receipt instance.
markdown
<string>
options
<string>
-c <chars>
: characters per line
24
-96
48
-l <language>
: language of receipt markdown text
en
, fr
, de
, es
, po
, it
, ru
, …: Multilingual (cp437, 852, 858, 866, 1252 characters)ja
: Japanese (shiftjis characters)ko
: Korean (ksc5601 characters)zh-hans
: Simplified Chinese (gb18030 characters)zh-hant
: Traditional Chinese (big5 characters)th
: Thai-s
: paper saving (reduce line spacing)The toPNG() instance method converts to PNG.
https://receiptline.github.io/receiptjs/test/topng.html
The toSVG() instance method converts to SVG.
https://receiptline.github.io/receiptjs/test/tosvg.html
The toText() instance method converts to plain text.
https://receiptline.github.io/receiptjs/test/totext.html
The toString() instance method returns a string representing the receipt markdown text.
The following files are required to use the Receipt Printer API.
<script type="text/javascript" src="receipt.js"></script>
<script type="text/javascript" src="receipt-printer.js"></script>
<script type="text/javascript" src="qrcode-generator/qrcode.js"></script>
const markdown = `^^^RECEIPT
03/18/2024, 12:34:56 PM
Asparagus | 1| 1.00
Broccoli | 2| 2.00
Carrot | 3| 3.00
---
^TOTAL | ^6.00`;
const receipt = Receipt.from(markdown, '-p generic -c 42');
const command = await receipt.toCommand();
The Receipt.from() static method creates a new Receipt instance.
markdown
<string>
options
<string>
-p <printer>
: printer control language
escpos
: ESC/POS (Epson)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) Experimentalstar
: StarPRNTstarline
: Star Line Modeemustarline
: Command Emulator Star Line Modestargraphic
: Star Graphic Modestarimpact
: Star Mode on dot impact printers Experimentalstarimpact2
: Star Mode on dot impact printers (Font 5x9 2P-1) Experimentalstarimpact3
: Star Mode on dot impact printers (Font 5x9 3P-1) Experimental-c <chars>
: characters per line
24
-96
48
-l <language>
: language of receipt markdown text
en
, fr
, de
, es
, po
, it
, ru
, …: Multilingual (cp437, 852, 858, 866, 1252 characters)ja
: Japanese (shiftjis characters)ko
: Korean (ksc5601 characters)zh-hans
: Simplified Chinese (gb18030 characters)zh-hant
: Traditional Chinese (big5 characters)th
: Thai-s
: paper saving (reduce line spacing)-m [<left>][,<right>]
: print margin
0
-24
0
-24
0,0
-u
: upside down-i
: print as image-n
: no paper cut-b <threshold>
: image thresholding
0
-255
-g <gamma>
: image gamma correction
0.1
-10.0
1.0
-v
: landscape orientation
escpos
, epson
, sii
, citizen
, star
-r <dpi>
:print resolution for ESC/POS, landscape, and device font
180
, 203
203
The toCommand() instance method converts to printer commands.
https://receiptline.github.io/receiptjs/test/tocommand.html
The following files are required to use the Receipt Serial API.
<script type="text/javascript" src="receipt.js"></script>
<script type="text/javascript" src="receipt-printer.js"></script>
<script type="text/javascript" src="receipt-serial.js"></script>
<script type="text/javascript" src="qrcode-generator/qrcode.js"></script>
const markdown = `^^^RECEIPT
03/18/2024, 12:34:56 PM
Asparagus | 1| 1.00
Broccoli | 2| 2.00
Carrot | 3| 3.00
---
^TOTAL | ^6.00`;
const conn = ReceiptSerial.connect({ baudRate: 19200 });
conn.on('status', status => {
console.log(status);
});
conn.on('ready', async () => {
const result = await conn.print(markdown, '-c 42');
});
The ReceiptSerial.connect() static method creates a new connection using the Web Serial API.
options
<object>
baudRate
: baud rate to establish serial communication
115200
none
8
1
hardware
These options are for real serial ports.
The receiptSerial.status instance property is a string representing the printer status.
online
: printer is onlineprint
: printer is printingcoveropen
: printer cover is openpaperempty
: no receipt papererror
: printer error (except cover open and paper empty)offline
: printer is off or offlinedisconnect
: printer is not connectedThe print() instance method prints a receipt markdown text.
https://receiptline.github.io/receiptjs/test/print.html
markdown
<string>
options
<string>
-c <chars>
: characters per line
24
-96
48
-l <language>
: language of receipt markdown text
en
, fr
, de
, es
, po
, it
, ru
, …: Multilingual (cp437, 852, 858, 866, 1252 characters)ja
: Japanese (shiftjis characters)ko
: Korean (ksc5601 characters)zh-hans
: Simplified Chinese (gb18030 characters)zh-hant
: Traditional Chinese (big5 characters)th
: Thai-s
: paper saving (reduce line spacing)-m [<left>][,<right>]
: print margin
0
-24
0
-24
0,0
-u
: upside down-i
: print as image-n
: no paper cut-b <threshold>
: image thresholding
0
-255
-g <gamma>
: image gamma correction
0.1
-10.0
1.0
-p <printer>
: printer control language
escpos
: ESC/POS (Epson)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) Experimentalstar
: StarPRNTstarline
: Star Line Modeemustarline
: Command Emulator Star Line Modestargraphic
: Star Graphic Modestarimpact
: Star Mode on dot impact printers Experimentalstarimpact2
: Star Mode on dot impact printers (Font 5x9 2P-1) Experimentalstarimpact3
: Star Mode on dot impact printers (Font 5x9 3P-1) Experimentalepson
, sii
, citizen
, fit
, impactb
, generic
, star
)-v
: landscape orientation
escpos
, epson
, sii
, citizen
, star
-r <dpi>
:print resolution for ESC/POS, landscape, and device font
180
, 203
203
success
: printing successprint
: printer is printingcoveropen
: printer cover is openpaperempty
: no receipt papererror
: printer error (except cover open and paper empty)offline
: printer is off or offlinedisconnect
: printer is not connectedThe receiptSerial.drawer instance property is a string representing the cash drawer status.
drawerclosed
: drawer is closeddraweropen
: drawer is openoffline
: printer is off or offlinedisconnect
: printer is not connectedThe invertDrawerState() instance method inverts cash drawer state.
invert
<boolean>
The close() instance method closes the connection.
The current implementation also closes other open connections.
The on() instance method adds the listener
function to the listeners array for the event named name
.
name
status
: printer status updatedready
: ready to printonline
: printer is onlineprint
: printer is printingcoveropen
: printer cover is openpaperempty
: no receipt papererror
: printer error (except cover open and paper empty)offline
: printer is off or offlinedisconnect
: printer is not connecteddrawer
: drawer status updateddrawerclosed
: drawer is closeddraweropen
: drawer is openlistener
The off() instance method removes the listener
function from the listeners array for the event named name
.
name
status
: printer status updatedready
: ready to printonline
: printer is onlineprint
: printer is printingcoveropen
: printer cover is openpaperempty
: no receipt papererror
: printer error (except cover open and paper empty)offline
: printer is off or offlinedisconnect
: printer is not connecteddrawer
: drawer status updateddrawerclosed
: drawer is closeddraweropen
: drawer is openlistener
The print function is available on Chrome, Edge, and Opera for PCs that support the Web Serial API.
(Windows, Linux, macOS, and ChromeOS)
Connect with the Web Serial API.
(Bluetooth, virtual serial port, and serial port)
Epson TM series (South Asia model) and Star MC series (StarPRNT model) can print with device font of Thai characters.
Closing the virtual port to which no printer is connected may cause the browser to stop responding.
Please change the signal line setting to “RS-232C cross cable”.
If the printer goes offline during print data transmission and the virtual port is closed, the browser may stop responding.
In this case, press the printer’s paper feed button.