react-thermal-printer
React for thermal printing. It is used to print to a thermal printer that supports ESC/POS commands using React. It provides a custom renderer to convert React elements to Uint8Array, you can easily markup the printing stuffs using React components.
Installation
yarn
yarn add react-thermal-printer
pnpm
pnpm add react-thermal-printer
npm
npm install --save react-thermal-printer
Usage
import { Br, Cut, Line, Printer, Text, Row, render } from 'react-thermal-printer';
const receipt = (
<Printer type="epson" width={42} characterSet="korea">
<Text size={{ width: 2, height: 2 }}>9,500μ</Text>
<Text bold={true}>κ²°μ μλ£</Text>
<Br />
<Line />
<Row left="κ²°μ λ°©λ²" right="체ν¬μΉ΄λ" />
<Row left="μΉ΄λλ²νΈ" right="123456**********" />
<Row left="ν λΆκΈ°κ°" right="μΌμλΆ" />
<Row left="κ²°μ κΈμ‘" right="9,500" />
<Row left="λΆκ°μΈμ‘" right="863" />
<Row left="곡κΈκ°μ‘" right="8,637" />
<Line />
<Row left="λ§μλ μ₯μμμμΌμ°¨ X 2" right="11,000" />
<Text>μ΅μ
1(500)/μ΅μ
2/λ©λͺ¨</Text>
<Row left="(-) ν μΈ" right="- 500" />
<Br />
<Line />
<Row left="ν©κ³" right="9,500" />
<Row left="(-) ν μΈ 2%" right="- 1,000" />
<Line />
<Row left="λν" right="κΉλν" />
<Row left="μ¬μ
μλ±λ‘λ²νΈ" right="000-00-00000" />
<Row left="λνλ²νΈ" right="0000-0000" />
<Row left="μ£Όμ" right="μ΄λμ μ΄λꡬ μ΄λλ λͺλλͺνΈ" />
<Line />
<Br />
<Text align="center">Wifi: some-wifi / PW: 123123</Text>
<Cut />
</Printer>
);
const data: Uint8Array = await render(receipt);
API
Components
<Printer>
Interface of thermal printer.
Requires type
to determine printer type.
<Printer type="epson">...</Printer>
<Printer type="epson" width={42}>...</Printer>
<Printer type="epson" characterSet="korea">...</Printer>
Note: Supported printer types are epson
, star
.
<Text>
Display text, and change text size or style to make it bold, underline, etc.
<Text>
component also allows <div>
element props.
<Text>text</Text>
<Text>fragment is {'allowed'}</Text>
<Text align="center">center text</Text>
<Text align="right">right text</Text>
<Text bold={true}>bold text</Text>
<Text underline="1dot-thick">underline text</Text>
<Text invert={true}>invert text</Text>
<Text size={{ width: 2, height: 2 }}>big size text</Text>
Note: <Text>
allows only text nodes.
<Row>
Display <Text>
on the left and right sides.
<Row left="left" right="right" />
<Row left="left" right="right" gap={2} />
<Row
left={<Text>left</Text>}
right="right"
/>
<Row
left={<Text>left</Text>}
right="very very long text will be multi line placed."
/>
<Br>
Feed line.
<Br />
<Line>
Draw line. Prints the character as much as the width
which from <Printer>
.
<Line />
<Line character="=" />
<Barcode>
Print barcode.
<Barcode type="UPC-A" content="111111111111" />
<Barcode type="CODE39" content="A000$" />
<Barcode align="center" type="UPC-A" content="111111111111" />
<QRCode>
Print qr code (2d barcode).
<QRCode content="https://github.com/seokju-na/react-thermal-printer" />
<QRCode align="center" content="https://github.com/seokju-na/react-thermal-printer" />
<Image>
Print image bitmap.
<Image src="https://my-cdn.com/image.png" />
<Image align="center" src="https://my-cdn.com/image.png" />
<Image src="https://my-cdn.com/image.png" reader={myCustomImageReader} />
function myCustomImageReader(
elem: ReactElement<ComponentProps<typeof Image>>
): Promise<Image>;
Apply greyscale(Floyd-Steinberg dithering):
import { transforms } from '@react-therma-printer/image';
<Image src="https://my-cdn.com/image.png" transforms={[transforms.floydSteinberg]} />
<Cut>
Cut the paper.
Perform full cutting, and feeds lines after cutting.
<Cut />
<Cut lineFeeds={6} />
<Raw>
Print raw data.
<Raw data={Uint8Array.from([0x00, 0x01, ...])} />
<Cashdraw>
Open cash drawer.
<Cashdraw pin="2pin" />
<Cashdraw pin="5pin" />
Functions
render
Returns: Promise<Uint8Array>
Render element to Uint8Array
data which corresponding to the esc/pos command.
Print via serial port (Web):
import { render, Printer, Text } from 'react-thermal-printer';
const data = await render(
<Printer type="epson">
<Text>Hello World</Text>
</Printer>
);
const port = await window.navigator.serial.requestPort();
await port.open({ baudRate: 9600 });
const writer = port.writable?.getWriter();
if (writer != null) {
await writer.write(data);
writer.releaseLock();
}
Print via network (Nodejs):
import { render, Printer, Text } from 'react-thermal-printer';
import { connect } from 'node:net';
const data = await render(
<Printer type="epson">
<Text>Hello World</Text>
</Printer>
);
const conn = connect({
host: '192.168.0.99',
port: 9100,
timeout: 3000,
}, () => {
conn.write(Buffer.from(data), () => {
conn.destroy();
});
});
License
MIT License