@makgabri/react-native-sunmi-printer v0.1.1
React-Native-Sunmi-Printer
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.
To implement after V1:
✓ 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
Installation
Using npm:
npm install --save @makgabri/react-native-sunmi-printeror using yarn:
yarn add @makgabri/react-native-sunmi-printerExample Import
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
Function returns
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
}Errors
| Error Code | Error Message |
|---|---|
| -1 | Printer service empty, please connect printer first |
| -2 | Remote exception error(refer to message to get more details) |
Table of Contents
- 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
connect
This function connects to the printer
Examples
// returns { success: true }
await SPrinter.connect();Returns Promise\ success message.
disconnect
This function disconnects the printer
Examples
// returns { success: true }
await SPrinter.disconnect();Returns Promise\ success message.
reset
This function resets any printing adjustments sent(e.g align or font size)
Examples
// returns { success: true }
await SPrinter.reset();Returns Promise\ success message.
testPrint
This function runs the default test print
Examples
// returns { success: true }
await SPrinter.testPrint();Returns Promise\ success message.
getPrinterSpecs
This function returns the printer specs including model, paper, printer version and serial no
Examples
// returns {DeviceModel: 'T2-GPIOINT\n', PrintPaper: '80mm', PrinterVersion: '1.05\n', SerialNo: 'XXXXXXXXXXXXXXXXXXXX'}
await SPrinter.getPrinterSpecs();Returns Promise\ Spec object.
getPrinterStatus
This function returns the printer current status such as printing or running
Examples
// returns "Printer is running"
await SPrinter.getPrinterStatus();Returns Promise<String> success message.
cutPaper
This function cuts the paper
Examples
// returns { success: true }
await SPrinter.cutPaper();Returns Promise\ success message.
printText
This function prints a given text
Parameters
textString text to print
Examples
// returns { success: true }
await SPrinter.printText("Example of printing");Returns Promise\ success message.
printTextCustom
This function prints a given text with styling of text
Parameters
textString text to printsizeNumber size of textisBoldboolean whether text is boldisUnderlineboolean whether text is underelinedtypefaceString typeface of text
Examples
// returns { success: true }
await SPrinter.printTextCustom("Example of custom printing", "28", true, true, "gh");Returns Promise\ success message.
printEmptyLines
This function prints a number of empty lines
Parameters
numNumber num of empty lines
Examples
// returns { success: true }
await SPrinter.printEmptyLines(3);Returns Promise\ success message.
setAlign
This function sets the alignment of next print
Parameters
alignALIGN align to set
Examples
// returns { success: true }
await SPrinter.setAlign(Constants.Align.CENTER);Returns Promise\ success message.
setFontSize
This function sets the font size of next print
Parameters
fontSizeNumber font size to set
Examples
// returns { success: true }
await SPrinter.setFontSize(28);Returns Promise\ success message.
printBarCode
This function prints a barcode given data
Parameters
dataString data of barcodesymbologyBARCODE_TYPE symbol of barcode typeheightNumber height of barcodewidthNumber width of barcodetextPositionBARCODE_TEXT_POSITION position of text
Examples
// returns { success: true }
await SPrinter.printBarCode("SecretABC", Constants.Barcode.CODE39, 90, 10, Constants.BarcodeText.ABOVE);Returns Promise\ success message.
printQRCode
This function prints a qr code given data
Parameters
dataString data of qr codemodulesizeNumber module size of qr codeerrorlevelQR_ERROR_LEVEL error level of qr code
Examples
// returns { success: true }
await SPrinter.printQRCode("URL TO SOME SECRET", 10, 15);Returns Promise\ success message.
printTable
This function prints a table given data
Parameters
textsArray<String> an array of texts to printwidthArray<Number> an array defining width of corresponding textalignArray<Number> an array defining align of corresponding text
Examples
// returns { success: true }
await SPrinter.printTable(['a','b','c'], [3,3,5], [1,2,0]);Returns Promise\ success message.
printBase64Image
This function prints an image given the base 64 string
Parameters
base64String base64 string of image
Examples
// returns { success: true }
await SPrinter.printBase64Image('some base 64 image string');Returns Promise\ success message.
ALIGN_CONSTANTS
Type: ALIGN
LEFT
Type: Number
CENTER
Type: Number
RIGHT
Type: Number
BARCODE_TYPE_CONSTANTS
Type: BARCODE_TYPE
UPCA
Type: Number
UPCE
Type: Number
JAN13
Type: Number
JAN8
Type: Number
CODE39
Type: Number
ITF
Type: Number
CODABAR
Type: Number
CODE93
Type: Number
CODE128
Type: Number
BARCODE_TEXT_POSITION_CONSTANTS
Type: BARCODE_TEXT_POSITION
NO_PRINT
Type: Number
ABOVE
Type: Number
BELOW
Type: Number
BOTH
Type: Number
QR_ERROR_LEVEL_CONSTANTS
Type: QR_ERROR_LEVEL
L
Type: Number
M
Type: Number
Q
Type: Number
H
Type: Number
License
MIT