receiptjs

Receipt.js

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();

example

Features

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

Convert to image or plain text

The following files are required to use the Receipt API.

<script type="text/javascript" src="receipt.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();

Receipt.from(markdown[, options])

The Receipt.from() static method creates a new Receipt instance.

Parameters

Return value

receipt.toPNG()

The toPNG() instance method converts to PNG.
https://receiptline.github.io/receiptjs/test/topng.html

Parameters

Return value

receipt.toSVG()

The toSVG() instance method converts to SVG.
https://receiptline.github.io/receiptjs/test/tosvg.html

Parameters

Return value

receipt.toText()

The toText() instance method converts to plain text.
https://receiptline.github.io/receiptjs/test/totext.html

Parameters

Return value

receipt.toString()

The toString() instance method returns a string representing the receipt markdown text.

Parameters

Return value

Convert to printer commands

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>
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();

Receipt.from(markdown[, options])

The Receipt.from() static method creates a new Receipt instance.

Parameters

Return value

receipt.toCommand()

The toCommand() instance method converts to printer commands.
https://receiptline.github.io/receiptjs/test/tocommand.html

Parameters

Return value

Print with the Web Serial API

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>
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');
});

ReceiptSerial.connect([options])

The ReceiptSerial.connect() static method creates a new connection using the Web Serial API.

Parameters

These options are for real serial ports.

Return value

receiptSerial.status

The receiptSerial.status instance property is a string representing the printer status.

Value

receiptSerial.print(markdown[, options])

The print() instance method prints a receipt markdown text.
https://receiptline.github.io/receiptjs/test/print.html

Parameters

Return value

receiptSerial.drawer

The receiptSerial.drawer instance property is a string representing the cash drawer status.

Value

receiptSerial.invertDrawerState(invert)

The invertDrawerState() instance method inverts cash drawer state.

Parameters

Return value

receiptSerial.close()

The close() instance method closes the connection.
The current implementation also closes other open connections.

Parameters

Return value

receiptSerial.on(name, listener)

The on() instance method adds the listener function to the listeners array for the event named name.

Parameters

Return value

receiptSerial.off(name, listener)

The off() instance method removes the listener function from the listeners array for the event named name.

Parameters

Return value

Web browsers

The print function is available on Chrome, Edge, and Opera that support the Web Serial API.
(Windows, Linux, macOS, ChromeOS, and Android)

Receipt printers

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.

Restrictions

The Web Serial API has no write timeout, so if hardware flow control is enabled, opening the printer cover during printing may cause the browser to stop responding. In this case, close the printer cover or press the paper feed button. Alternatively, change the printer’s busy condition setting from “Offline or receive buffer full” to “Receive buffer full”.

The restriction on Epson TM Virtual Port on Windows that existed in previous versions has been resolved. There is no need to change the signal line settings.