An unofficial React Native library for printing on an EPSON TM printer with the Epson ePOS SDK for iOS and Epson ePOS SDK for Android
npm install react-native-esc-pos-printer
or
yarn add react-native-esc-pos-printer
Add the following permissions to android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
For Android >= 10 add:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
pod install
When the Bluetooth or USB is used, set the protocol name. Set the protocol name according to the following procedure:
- In Project Navigator, select *.plist. (The file name will be Project name-info.)
- In the pop-up menu, select Add Row.
- Select "Supported external accessory protocols".
- Expand the items added in Step 3.
- Enter com.epson.escpos as the Value for Item 0.
- init
- discover
- printRawData
- pairingBluetoothPrinter
- getPrinterCharsPerLine
- startMonitorPrinter
- startMonitorPrinter
- printing
Initializes printer using it's target and series name.
Name | Type | Required | Description |
---|---|---|---|
target |
string |
Yes |
The connection target of a device which can be specified by connectAPI: ("TCP:192.168.192.168" "BT:00:22:15:7D:70:9C" "USB:000000000000000000") |
seriesName |
string |
Yes |
Specifies the target printer model. |
import EscPosPrinter from 'react-native-esc-pos-printer';
EscPosPrinter.init({
target: 'TCP:192.168.192.168',
seriesName: 'EPOS2_TM_M10',
})
.then(() => console.log('Init success!'))
.catch((e) => console.log('Init error:', e.message));
For iOS you must pair printer with device to search Bluetooth printers
Starts searching for device. Returns list of printers.
Name | Type | Required | Description |
---|---|---|---|
usbSerialNumber |
boolean |
No |
To extract the serial number of the usb device on Android |
interface IPrinter {
name: string;
ip: string;
mac: string;
target: string;
bt: string;
usb: string;
}
import EscPosPrinter from 'react-native-esc-pos-printer';
EscPosPrinter.discovery()
.then((printers) => {
console.log(printers[0]);
/*
{
name: "TM_M10",
ip: "192.168.192.168" or "",
mac: "12:34:56:78:56:78" or "",
target: "TCP:192.168.192.168" or "BT:00:22:15:7D:70:9C" or "USB:000000000000000000",
bt: "12:34:56:78:56:78" or "",
usb: "000000000000000000" or "";
usbSerialNumber: "123456789012345678" or ""; // available if usbSerialNumber === true
}
*/
})
.catch((e) => console.log('Print error:', e.message));
import EscPosPrinter from 'react-native-esc-pos-printer';
EscPosPrinter.discovery({ usbSerialNumber: true })
.then((printers) => {
console.log(printers[0]);
/*
{
name: "TM_M10",
ip: "192.168.192.168" or "",
mac: "12:34:56:78:56:78" or "",
target: "TCP:192.168.192.168" or "BT:00:22:15:7D:70:9C" or "USB:000000000000000000",
bt: "12:34:56:78:56:78" or "",
usb: "000000000000000000" or "",
usbSerialNumber: "123456789012345678" or ""
};
}
*/
})
.catch((e) => console.log('Print error:', e.message));
Prints with the given binary data (Uint8Array)
Name | Type | Required | Description |
---|---|---|---|
binaryData |
Uint8Array |
Yes |
string representing in JS Uint8Array data |
interface IMonitorStatus {
connection: string;
online: string;
coverOpen: string;
paper: string;
paperFeed: string;
panelSwitch: string;
drawer: string;
errorStatus: string;
autoRecoverErr: string;
adapter: string;
batteryLevel: string;
}
import EscPosPrinter, {
getPrinterSeriesByName,
} from 'react-native-esc-pos-printer';
import Encoder from 'esc-pos-encoder';
const encoder = new Encoder();
encoder
.initialize()
.line('The quick brown fox jumps over the lazy dog')
.newline()
.newline()
.newline()
.cut('partial');
let initialized = false;
if (!initialized) {
const { target, name } = printer;
await EscPosPrinter.init({
target: target,
seriesName: getPrinterSeriesByName(name),
});
initialized = true;
}
const status = await EscPosPrinter.printRawData(encoder.encode());
console.log('Print success!', status);
Shows a list of Bluetooth devices available for pairing and pairs a selected device with the terminal. Opens native dialog.
import { pairingBluetoothPrinter } from 'react-native-esc-pos-printer';
EscPosPrinter.pairingBluetoothPrinter()
.then(() => console.log('pairing success!'))
.catch((e) => console.log('pairing error:', e.message));
Returns max characters per line for given printer series (Usefull while building receipt layout).
Supports only font A
for now.
Name | Type | Required | Description |
---|---|---|---|
seriesName |
string |
Yes |
Specifies the target printer model. |
{
fontA: number;
}
import EscPosPrinter, {
getPrinterSeriesByName,
} from 'react-native-esc-pos-printer';
const { name } = printer;
EscPosPrinter.getPrinterCharsPerLine(getPrinterSeriesByName(name))
.then((result) => console.log(result)) // { fontA: 48 }
.catch((e) => console.log('error:', e.message));
Monitors printer status with a given interval in seconds.
import EscPosPrinter from 'react-native-esc-pos-printer';
EscPosPrinter.addPrinterStatusListener((status) => {
console.log(status.connection, status.online, status.paper); // will be executed every 5 sec
});
EscPosPrinter.startMonitorPrinter(5)
.then(() => console.log('Start monitor success!'))
.catch((e) => console.log('Start monitor error:', e.message));
Monitors printer status with a given interval in seconds.
import EscPosPrinter from 'react-native-esc-pos-printer';
EscPosPrinter.stopMonitorPrinter()
.then(() => console.log('Stopped!'))
.catch((e) => console.log('Stop error:', e.message));
Initializes printing class for chained printing.
import EscPosPrinter from 'react-native-esc-pos-printer';
const printing = new EscPosPrinter.printing();
printing
.initialize()
.align('center')
.size(6, 6)
.line('DUDE!')
.size(1, 1)
.text('is that a ')
.bold()
.underline()
.text('printer?')
.bold()
.underline()
.newline(2)
.align('center')
.image(image, 200)
.cut()
.send()
.then(() => console.log('Stopped!'))
.catch((e) => console.log('Stop error:', e.message));
Initializes the chaining object - this must be the first call in the chain
printing.initialize().text('printer?');
Prints text
printing.initialize().text('printer?');
Prints n numbers of newlines, defaults to 1 newline
printing.initialize().newline(n);
Prints text followed by a new line
printing.initialize().line('Cats eating mangos right off the tree');
Sets the underline state - optional boolean parameter, if no parameter is supplied it will toggle the underline state
printing.initialize().underline();
Sets the bold state - optional boolean parameter, if no parameter is supplied it will toggle the bold state
printing.initialize().bold();
Sets the text size. Text can be scaled from size 1 - 8. Optional width parameter, if not provided text will be scaled equal to the height.
printing.initialize().size(5, 4);
Sets the text alignment. Valid values are 'left' | 'center' | 'right'.
printing.initialize().align('center');
Prints an image. Accepts a base64 encoded string and a width of the image.
printing.initialize().image('base64ImageString', 200);
Adds a cut to the paper
printing.initialize().cut();
Is required at the end of a printer chain to send the commands to the printer
printing.initialize().text("hello, is it me you're looking for").send();
- For now it's not possible to print and discover on Android simulator. But you can always use real device.
- For now you can print just using Uint8Array. Fortunately it's quite easy with https://www.npmjs.com/package/esc-pos-encoder.
- You can not print images for now but work in progress ;)
- Export all build in mehods including print image
- Make possible to print on Android simulator
MIT