Sunmi devices have built in printers. This library extends the java code such that the functions can be used in react native. More over, all sunmi devices(at least printers) run on android so this library is only available for android.
✓ Basic printer connection
✓ Test printing
✓ Get printer status and specs
✓ Print text, set align and bold
✓ Print images
✗ Set bold
✗ Print bar code
✗ Print tables
✗ Print QR codes
✗ Checking params for printing qrcode, tables and barcode
Using npm:
npm install --save @makgabri/react-native-sunmi-printer
or using yarn:
yarn add @makgabri/react-native-sunmi-printer
import { SPrinter, Constants } from '@makgabri/react-native-sunmi-printer';
// ...
await SPrinter.connect();
await SPrinter.testPrint();
await SPrinter.disconnect();
Below is the api documentation to execute other functions in the library including printing
- texts
- images
- setting align/bold
- ...etc
All functions run async as the printer may need time to respond. When successful or a json object is returned such as the case for getting specs, the promise will resolve an object. Example:
try {
// Successful response
await SPrinter.connect();
// { successful: true }
// Successfull spec response
await SPrinter.getPrinterSpecs();
//{DeviceModel: 'T2-GPIOINT\n', PrintPaper: '80mm', PrinterVersion: '1.05\n', SerialNo: 'XXXXXXXXXXXXXXXXXXXX'}
} catch(e) {
console.log(e.code) // Code for error
console.log(e.message) // Message for error
}
Error Code | Error Message |
---|---|
-1 | Printer service empty, please connect printer first |
-2 | Remote exception error(refer to message to get more details) |
- connect
- disconnect
- reset
- testPrint
- getPrinterSpecs
- getPrinterStatus
- cutPaper
- printText
- printTextCustom
- printEmptyLines
- setAlign
- setFontSize
- printBarCode
- printQRCode
- printTable
- printBase64Image
- ALIGN_CONSTANTS
- BARCODE_TYPE_CONSTANTS
- BARCODE_TEXT_POSITION_CONSTANTS
- QR_ERROR_LEVEL_CONSTANTS
This function connects to the printer
// returns { success: true }
await SPrinter.connect();
Returns Promise<any> success message.
This function disconnects the printer
// returns { success: true }
await SPrinter.disconnect();
Returns Promise<any> success message.
This function resets any printing adjustments sent(e.g align or font size)
// returns { success: true }
await SPrinter.reset();
Returns Promise<any> success message.
This function runs the default test print
// returns { success: true }
await SPrinter.testPrint();
Returns Promise<any> success message.
This function returns the printer specs including model, paper, printer version and serial no
// returns {DeviceModel: 'T2-GPIOINT\n', PrintPaper: '80mm', PrinterVersion: '1.05\n', SerialNo: 'XXXXXXXXXXXXXXXXXXXX'}
await SPrinter.getPrinterSpecs();
Returns Promise<any> Spec object.
This function returns the printer current status such as printing or running
// returns "Printer is running"
await SPrinter.getPrinterStatus();
Returns Promise<String> success message.
This function cuts the paper
// returns { success: true }
await SPrinter.cutPaper();
Returns Promise<any> success message.
This function prints a given text
-
text
String text to print
// returns { success: true }
await SPrinter.printText("Example of printing");
Returns Promise<any> success message.
This function prints a given text with styling of text
-
text
String text to print -
size
Number size of text -
isBold
boolean whether text is bold -
isUnderline
boolean whether text is underelined -
typeface
String typeface of text
// returns { success: true }
await SPrinter.printTextCustom("Example of custom printing", "28", true, true, "gh");
Returns Promise<any> success message.
This function prints a number of empty lines
-
num
Number num of empty lines
// returns { success: true }
await SPrinter.printEmptyLines(3);
Returns Promise<any> success message.
This function sets the alignment of next print
-
align
ALIGN align to set
// returns { success: true }
await SPrinter.setAlign(Constants.Align.CENTER);
Returns Promise<any> success message.
This function sets the font size of next print
-
fontSize
Number font size to set
// returns { success: true }
await SPrinter.setFontSize(28);
Returns Promise<any> success message.
This function prints a barcode given data
-
data
String data of barcode -
symbology
BARCODE_TYPE symbol of barcode type -
height
Number height of barcode -
width
Number width of barcode -
textPosition
BARCODE_TEXT_POSITION position of text
// returns { success: true }
await SPrinter.printBarCode("SecretABC", Constants.Barcode.CODE39, 90, 10, Constants.BarcodeText.ABOVE);
Returns Promise<any> success message.
This function prints a qr code given data
-
data
String data of qr code -
modulesize
Number module size of qr code -
errorlevel
QR_ERROR_LEVEL error level of qr code
// returns { success: true }
await SPrinter.printQRCode("URL TO SOME SECRET", 10, 15);
Returns Promise<any> success message.
This function prints a table given data
-
texts
Array<String> an array of texts to print -
width
Array<Number> an array defining width of corresponding text -
align
Array<Number> an array defining align of corresponding text
// returns { success: true }
await SPrinter.printTable(['a','b','c'], [3,3,5], [1,2,0]);
Returns Promise<any> success message.
This function prints an image given the base 64 string
-
base64
String base64 string of image
// returns { success: true }
await SPrinter.printBase64Image('some base 64 image string');
Returns Promise<any> success message.
Type: ALIGN
Type: Number
Type: Number
Type: Number
Type: BARCODE_TYPE
Type: Number
Type: Number
Type: Number
Type: Number
Type: Number
Type: Number
Type: Number
Type: Number
Type: Number
Type: BARCODE_TEXT_POSITION
Type: Number
Type: Number
Type: Number
Type: Number
Type: QR_ERROR_LEVEL
Type: Number
Type: Number
Type: Number
Type: Number
MIT