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

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

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

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 for PCs that support the Web Serial API.
(Windows, Linux, macOS, and ChromeOS)

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

When connecting a USB printer with Epson TM Virtual Port on Windows

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”.

tmvpd

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.

License