react-native-thermal-receipt-printer-image-qr
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -1,81 +0,334 @@ | ||
import { NativeEventEmitter } from "react-native"; | ||
import {NativeEventEmitter} from "react-native"; | ||
export interface PrinterOptions { | ||
beep?: boolean; | ||
cut?: boolean; | ||
tailingLine?: boolean; | ||
encoding?: string; | ||
beep?: boolean; | ||
cut?: boolean; | ||
tailingLine?: boolean; | ||
encoding?: string; | ||
} | ||
declare const COMMANDS: { | ||
LF: string; | ||
ESC: string; | ||
FS: string; | ||
GS: string; | ||
US: string; | ||
FF: string; | ||
DLE: string; | ||
DC1: string; | ||
DC4: string; | ||
EOT: string; | ||
NUL: string; | ||
EOL: string; | ||
HORIZONTAL_LINE: { | ||
HR_58MM: string; | ||
HR2_58MM: string; | ||
HR3_58MM: string; | ||
HR_80MM: string; | ||
HR2_80MM: string; | ||
HR3_80MM: string; | ||
}; | ||
FEED_CONTROL_SEQUENCES: { | ||
/** | ||
* Print and line feed | ||
*/ | ||
CTL_LF: string; | ||
/** | ||
* Form feed | ||
*/ | ||
CTL_FF: string; | ||
/** | ||
* Carriage return | ||
*/ | ||
CTL_CR: string; | ||
/** | ||
* Horizontal tab | ||
*/ | ||
CTL_HT: string; | ||
/** | ||
* Vertical tab | ||
*/ | ||
CTL_VT: string; | ||
}; | ||
LINE_SPACING: { | ||
LS_DEFAULT: string; | ||
LS_SET: string; | ||
LS_SET1: string; | ||
}; | ||
HARDWARE: { | ||
/** | ||
* Clear data in buffer and reset modes | ||
*/ | ||
HW_INIT: string; | ||
/** | ||
* Printer select | ||
*/ | ||
HW_SELECT: string; | ||
/** | ||
* Reset printer hardware | ||
*/ | ||
HW_RESET: string; | ||
}; | ||
CASH_DRAWER: { | ||
/** | ||
* Sends a pulse to pin 2 [] | ||
*/ | ||
CD_KICK_2: string; | ||
/** | ||
* ends a pulse to pin 5 [] | ||
*/ | ||
CD_KICK_5: string; | ||
}; | ||
MARGINS: { | ||
/** | ||
* Fix bottom size | ||
*/ | ||
BOTTOM: string; | ||
/** | ||
* Fix left size | ||
*/ | ||
LEFT: string; | ||
/** | ||
* Fix right size | ||
*/ | ||
RIGHT: string; | ||
}; | ||
PAPER: { | ||
/** | ||
* Full cut paper | ||
*/ | ||
PAPER_FULL_CUT: string; | ||
/** | ||
* Partial cut paper | ||
*/ | ||
PAPER_PART_CUT: string; | ||
/** | ||
* Partial cut paper | ||
*/ | ||
PAPER_CUT_A: string; | ||
/** | ||
* Partial cut paper | ||
*/ | ||
PAPER_CUT_B: string; | ||
}; | ||
TEXT_FORMAT: { | ||
/** | ||
* Normal text | ||
*/ | ||
TXT_NORMAL: string; | ||
/** | ||
* Double height text | ||
*/ | ||
TXT_2HEIGHT: string; | ||
/** | ||
* Double width text | ||
*/ | ||
TXT_2WIDTH: string; | ||
/** | ||
* Double width & height text | ||
*/ | ||
TXT_4SQUARE: string; | ||
/** | ||
* other sizes | ||
*/ | ||
TXT_CUSTOM_SIZE: (width: number, height: number) => string; | ||
TXT_HEIGHT: { | ||
1: string; | ||
2: string; | ||
3: string; | ||
4: string; | ||
5: string; | ||
6: string; | ||
7: string; | ||
8: string; | ||
}; | ||
TXT_WIDTH: { | ||
1: string; | ||
2: string; | ||
3: string; | ||
4: string; | ||
5: string; | ||
6: string; | ||
7: string; | ||
8: string; | ||
}; | ||
/** | ||
* Underline font OFF | ||
*/ | ||
TXT_UNDERL_OFF: string; | ||
/** | ||
* Underline font 1-dot ON | ||
*/ | ||
TXT_UNDERL_ON: string; | ||
/** | ||
* Underline font 2-dot ON | ||
*/ | ||
TXT_UNDERL2_ON: string; | ||
/** | ||
* Bold font OFF | ||
*/ | ||
TXT_BOLD_OFF: string; | ||
/** | ||
* Bold font ON | ||
*/ | ||
TXT_BOLD_ON: string; | ||
/** | ||
* Italic font ON | ||
*/ | ||
TXT_ITALIC_OFF: string; | ||
/** | ||
* Italic font ON | ||
*/ | ||
TXT_ITALIC_ON: string; | ||
/** | ||
* Font type A | ||
*/ | ||
TXT_FONT_A: string; | ||
/** | ||
* Font type B | ||
*/ | ||
TXT_FONT_B: string; | ||
/** | ||
* Font type C | ||
*/ | ||
TXT_FONT_C: string; | ||
/** | ||
* Left justification | ||
*/ | ||
TXT_ALIGN_LT: string; | ||
/** | ||
* Centering | ||
*/ | ||
TXT_ALIGN_CT: string; | ||
/** | ||
* Right justification | ||
*/ | ||
TXT_ALIGN_RT: string; | ||
}; | ||
}; | ||
export interface PrinterImageOptions { | ||
beep?: boolean; | ||
cut?: boolean; | ||
tailingLine?: boolean; | ||
encoding?: string; | ||
imageWidth?: number; | ||
paddingX?: number; | ||
beep?: boolean; | ||
cut?: boolean; | ||
tailingLine?: boolean; | ||
encoding?: string; | ||
/** | ||
* ios only | ||
*/ | ||
imageWidth?: number; | ||
paddingX?: number; | ||
} | ||
export interface IUSBPrinter { | ||
device_name: string; | ||
vendor_id: string; | ||
product_id: string; | ||
device_name: string; | ||
vendor_id: string; | ||
product_id: string; | ||
} | ||
export interface IBLEPrinter { | ||
device_name: string; | ||
inner_mac_address: string; | ||
device_name: string; | ||
inner_mac_address: string; | ||
} | ||
export interface INetPrinter { | ||
device_name: string; | ||
host: string; | ||
port: number; | ||
device_name: string; | ||
host: string; | ||
port: number; | ||
} | ||
export declare enum ColumnAliment { | ||
LEFT = 0, | ||
CENTER = 1, | ||
RIGHT = 2 | ||
LEFT = 0, | ||
CENTER = 1, | ||
RIGHT = 2 | ||
} | ||
export declare const USBPrinter: { | ||
init: () => Promise<void>; | ||
getDeviceList: () => Promise<IUSBPrinter[]>; | ||
connectPrinter: (vendorId: string, productId: string) => Promise<IUSBPrinter>; | ||
closeConn: () => Promise<void>; | ||
printText: (text: string, opts?: PrinterOptions) => void; | ||
printBill: (text: string, opts?: PrinterOptions) => void; | ||
printImage: (imgUrl: string, opts?: PrinterImageOptions) => void; | ||
printQrCode: (qrCodeBase64: string, opts?: PrinterImageOptions) => void; | ||
printRaw: (text: string) => void; | ||
printColumnsText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], opts?: PrinterOptions) => void; | ||
init: () => Promise<void>; | ||
getDeviceList: () => Promise<IUSBPrinter[]>; | ||
connectPrinter: (vendorId: string, productId: string) => Promise<IUSBPrinter>; | ||
closeConn: () => Promise<void>; | ||
printText: (text: string, opts?: PrinterOptions) => void; | ||
printBill: (text: string, opts?: PrinterOptions) => void; | ||
/** | ||
* image url | ||
* @param imgUrl | ||
* @param opts | ||
*/ | ||
printImage: (imgUrl: string, opts?: PrinterImageOptions) => void; | ||
/** | ||
* base64string, except -> data:image/png;base64, | ||
* @param qrCodeBase64 | ||
* @param opts | ||
*/ | ||
printQrCode: (qrCodeBase64: string, opts?: PrinterImageOptions) => void; | ||
/** | ||
* android print with encoder | ||
* @param text | ||
*/ | ||
printRaw: (text: string) => void; | ||
printColumnsText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], columnStyle: string[], opts?: PrinterOptions) => void; | ||
}; | ||
export declare const BLEPrinter: { | ||
init: () => Promise<void>; | ||
getDeviceList: () => Promise<IBLEPrinter[]>; | ||
connectPrinter: (inner_mac_address: string) => Promise<IBLEPrinter>; | ||
closeConn: () => Promise<void>; | ||
printText: (text: string, opts?: PrinterOptions) => void; | ||
printBill: (text: string, opts?: PrinterOptions) => void; | ||
printImage: (imgUrl: string, opts?: PrinterImageOptions) => void; | ||
printQrCode: (qrCodeBase64: string, opts?: PrinterImageOptions) => void; | ||
printRaw: (text: string) => void; | ||
printColumnsText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], opts?: PrinterOptions) => void; | ||
init: () => Promise<void>; | ||
getDeviceList: () => Promise<IBLEPrinter[]>; | ||
connectPrinter: (inner_mac_address: string) => Promise<IBLEPrinter>; | ||
closeConn: () => Promise<void>; | ||
printText: (text: string, opts?: PrinterOptions) => void; | ||
printBill: (text: string, opts?: PrinterOptions) => void; | ||
/** | ||
* image url | ||
* @param imgUrl | ||
* @param opts | ||
*/ | ||
printImage: (imgUrl: string, opts?: PrinterImageOptions) => void; | ||
/** | ||
* base64string, except -> data:image/png;base64, | ||
* @param qrCodeBase64 | ||
* @param opts | ||
*/ | ||
printQrCode: (qrCodeBase64: string, opts?: PrinterImageOptions) => void; | ||
/** | ||
* android print with encoder | ||
* @param text | ||
*/ | ||
printRaw: (text: string) => void; | ||
printColumnsText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], columnStyle: string[], opts?: PrinterOptions) => void; | ||
}; | ||
export declare const NetPrinter: { | ||
init: () => Promise<void>; | ||
getDeviceList: () => Promise<INetPrinter[]>; | ||
connectPrinter: (host: string, port: number) => Promise<INetPrinter>; | ||
closeConn: () => Promise<void>; | ||
printText: (text: string, opts?: {}) => void; | ||
printBill: (text: string, opts?: {}) => void; | ||
printImage: (imgUrl: string, opts?: PrinterImageOptions) => void; | ||
printQrCode: (qrCodeBase64: string, opts?: PrinterImageOptions) => void; | ||
printRaw: (text: string) => void; | ||
/** | ||
* `columnWidth` | ||
* 80mm => 46 character | ||
* 58mm => ... | ||
*/ | ||
printColumnsText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], opts?: PrinterOptions) => void; | ||
init: () => Promise<void>; | ||
getDeviceList: () => Promise<INetPrinter[]>; | ||
connectPrinter: (host: string, port: number) => Promise<INetPrinter>; | ||
closeConn: () => Promise<void>; | ||
printText: (text: string, opts?: {}) => void; | ||
printBill: (text: string, opts?: PrinterOptions) => void; | ||
/** | ||
* image url | ||
* @param imgUrl | ||
* @param opts | ||
*/ | ||
printImage: (imgUrl: string, opts?: PrinterImageOptions) => void; | ||
/** | ||
* base64string, except -> data:image/png;base64, | ||
* @param qrCodeBase64 | ||
* @param opts | ||
*/ | ||
printQrCode: (qrCodeBase64: string, opts?: PrinterImageOptions) => void; | ||
/** | ||
* Android print with encoder | ||
* @param text | ||
*/ | ||
printRaw: (text: string) => void; | ||
/** | ||
* `columnWidth` | ||
* 80mm => 46 character | ||
* 58mm => 30 character | ||
*/ | ||
printColumnsText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], columnStyle: string[], opts?: PrinterOptions) => void; | ||
}; | ||
export declare const NetPrinterEventEmitter: NativeEventEmitter; | ||
export declare enum RN_THERMAL_RECEIPT_PRINTER_EVENTS { | ||
EVENT_NET_PRINTER_SCANNED_SUCCESS = "scannerResolved", | ||
EVENT_NET_PRINTER_SCANNING = "scannerRunning", | ||
EVENT_NET_PRINTER_SCANNED_ERROR = "registerError" | ||
EVENT_NET_PRINTER_SCANNED_SUCCESS = "scannerResolved", | ||
EVENT_NET_PRINTER_SCANNING = "scannerRunning", | ||
EVENT_NET_PRINTER_SCANNED_ERROR = "registerError" | ||
} |
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
__assign = Object.assign || function (t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
@@ -12,8 +12,11 @@ s = arguments[i]; | ||
}; | ||
import { NativeModules, NativeEventEmitter, Platform } from "react-native"; | ||
import {NativeModules, NativeEventEmitter, Platform} from "react-native"; | ||
import * as EPToolkit from "./utils/EPToolkit"; | ||
import { processColumnText } from './utils/print-column'; | ||
import {processColumnText} from './utils/print-column'; | ||
import PRINTER_COMMANDS from './utils/printer-commands'; | ||
var RNUSBPrinter = NativeModules.RNUSBPrinter; | ||
var RNBLEPrinter = NativeModules.RNBLEPrinter; | ||
var RNNetPrinter = NativeModules.RNNetPrinter; | ||
export var COMMANDS = PRINTER_COMMANDS; | ||
export var ColumnAliment; | ||
@@ -48,6 +51,11 @@ (function (ColumnAliment) { | ||
}; | ||
var textPreprocessingIOS = function (text, canCut) { | ||
if (canCut === void 0) { canCut = true; } | ||
var textPreprocessingIOS = function (text, canCut, beep) { | ||
if (canCut === void 0) { | ||
canCut = true; | ||
} | ||
if (beep === void 0) { | ||
beep = true; | ||
} | ||
var options = { | ||
beep: true, | ||
beep: beep, | ||
cut: canCut, | ||
@@ -74,3 +82,7 @@ }; | ||
return new Promise(function (resolve, reject) { | ||
return RNUSBPrinter.init(function () { return resolve(); }, function (error) { return reject(error); }); | ||
return RNUSBPrinter.init(function () { | ||
return resolve(); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -80,3 +92,7 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
return RNUSBPrinter.getDeviceList(function (printers) { return resolve(printers); }, function (error) { return reject(error); }); | ||
return RNUSBPrinter.getDeviceList(function (printers) { | ||
return resolve(printers); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -86,3 +102,7 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
return RNUSBPrinter.connectPrinter(vendorId, productId, function (printer) { return resolve(printer); }, function (error) { return reject(error); }); | ||
return RNUSBPrinter.connectPrinter(vendorId, productId, function (printer) { | ||
return resolve(printer); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -97,3 +117,5 @@ }, | ||
printText: function (text, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return RNUSBPrinter.printRawData(textTo64Buffer(text, opts), function (error) { | ||
@@ -104,3 +126,5 @@ return console.warn(error); | ||
printBill: function (text, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return RNUSBPrinter.printRawData(billTo64Buffer(text, opts), function (error) { | ||
@@ -110,27 +134,47 @@ return console.warn(error); | ||
}, | ||
//image url | ||
/** | ||
* image url | ||
* @param imgUrl | ||
* @param opts | ||
*/ | ||
printImage: function (imgUrl, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
RNUSBPrinter.printImageData(imgUrl, opts, function (error) { return console.warn(error); }); | ||
RNUSBPrinter.printImageData(imgUrl, opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNUSBPrinter.printImageData(imgUrl, function (error) { | ||
return console.warn(error); | ||
}); | ||
} | ||
else { | ||
RNUSBPrinter.printImageData(imgUrl, function (error) { return console.warn(error); }); | ||
} | ||
}, | ||
// base64string, except -> data:image/png;base64, | ||
/** | ||
* base64string, except -> data:image/png;base64, | ||
* @param qrCodeBase64 | ||
* @param opts | ||
*/ | ||
printQrCode: function (qrCodeBase64, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
RNUSBPrinter.printQrCode(qrCodeBase64, opts, function (error) { return console.warn(error); }); | ||
RNUSBPrinter.printQrCode(qrCodeBase64, opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNUSBPrinter.printQrCode(qrCodeBase64, function (error) { | ||
return console.warn(error); | ||
}); | ||
} | ||
else { | ||
RNUSBPrinter.printQrCode(qrCodeBase64, function (error) { return console.warn(error); }); | ||
} | ||
}, | ||
// android print with encoder | ||
/** | ||
* android print with encoder | ||
* @param text | ||
*/ | ||
printRaw: function (text) { | ||
if (Platform.OS === "ios") { | ||
} | ||
else { | ||
} else { | ||
RNUSBPrinter.printRawData(text, function (error) { | ||
@@ -141,5 +185,7 @@ return console.warn(error); | ||
}, | ||
printColumnsText: function (texts, columnWidth, columnAliment, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var result = processColumnText(texts, columnWidth, columnAliment); | ||
printColumnsText: function (texts, columnWidth, columnAliment, columnStyle, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var result = processColumnText(texts, columnWidth, columnAliment, columnStyle); | ||
RNUSBPrinter.printRawData(textTo64Buffer(result, opts), function (error) { | ||
@@ -153,3 +199,7 @@ return console.warn(error); | ||
return new Promise(function (resolve, reject) { | ||
return RNBLEPrinter.init(function () { return resolve(); }, function (error) { return reject(error); }); | ||
return RNBLEPrinter.init(function () { | ||
return resolve(); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -159,3 +209,7 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
return RNBLEPrinter.getDeviceList(function (printers) { return resolve(printers); }, function (error) { return reject(error); }); | ||
return RNBLEPrinter.getDeviceList(function (printers) { | ||
return resolve(printers); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -165,3 +219,7 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
return RNBLEPrinter.connectPrinter(inner_mac_address, function (printer) { return resolve(printer); }, function (error) { return reject(error); }); | ||
return RNBLEPrinter.connectPrinter(inner_mac_address, function (printer) { | ||
return resolve(printer); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -176,8 +234,11 @@ }, | ||
printText: function (text, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
var processedText = textPreprocessingIOS(text, false); | ||
RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); | ||
} | ||
else { | ||
var processedText = textPreprocessingIOS(text, false, false); | ||
RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNBLEPrinter.printRawData(textTo64Buffer(text, opts), function (error) { | ||
@@ -189,8 +250,12 @@ return console.warn(error); | ||
printBill: function (text, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var _a, _b; | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
var processedText = textPreprocessingIOS(text); | ||
RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); | ||
} | ||
else { | ||
var processedText = textPreprocessingIOS(text, (_a = opts === null || opts === void 0 ? void 0 : opts.cut) !== null && _a !== void 0 ? _a : true, (_b = opts.beep) !== null && _b !== void 0 ? _b : true); | ||
RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNBLEPrinter.printRawData(billTo64Buffer(text, opts), function (error) { | ||
@@ -201,27 +266,56 @@ return console.warn(error); | ||
}, | ||
//image url | ||
/** | ||
* image url | ||
* @param imgUrl | ||
* @param opts | ||
*/ | ||
printImage: function (imgUrl, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
RNBLEPrinter.printImageData(imgUrl, opts, function (error) { return console.warn(error); }); | ||
/** | ||
* just development | ||
*/ | ||
RNBLEPrinter.printImageData(imgUrl, opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNBLEPrinter.printImageData(imgUrl, function (error) { | ||
return console.warn(error); | ||
}); | ||
} | ||
else { | ||
// RNNetPrinter.printImageData(imgUrl, (error: Error) => console.warn(error)); | ||
} | ||
}, | ||
// base64string, except -> data:image/png;base64, | ||
/** | ||
* base64string, except -> data:image/png;base64, | ||
* @param qrCodeBase64 | ||
* @param opts | ||
*/ | ||
printQrCode: function (qrCodeBase64, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
RNBLEPrinter.printQrCode(qrCodeBase64, opts, function (error) { return console.warn(error); }); | ||
/** | ||
* just development | ||
*/ | ||
RNBLEPrinter.printQrCode(qrCodeBase64, opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
/** | ||
* just development | ||
*/ | ||
RNBLEPrinter.printQrCode(qrCodeBase64, function (error) { | ||
return console.warn(error); | ||
}); | ||
} | ||
else { | ||
// RNNetPrinter.printQrCode(qrCodeBase64, (error: Error) => console.warn(error)); | ||
} | ||
}, | ||
// android print with encoder | ||
/** | ||
* android print with encoder | ||
* @param text | ||
*/ | ||
printRaw: function (text) { | ||
if (Platform.OS === "ios") { | ||
} | ||
else { | ||
} else { | ||
RNBLEPrinter.printRawData(text, function (error) { | ||
@@ -232,10 +326,13 @@ return console.warn(error); | ||
}, | ||
printColumnsText: function (texts, columnWidth, columnAliment, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var result = processColumnText(texts, columnWidth, columnAliment); | ||
printColumnsText: function (texts, columnWidth, columnAliment, columnStyle, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var result = processColumnText(texts, columnWidth, columnAliment, columnStyle); | ||
if (Platform.OS === "ios") { | ||
var processedText = textPreprocessingIOS(result, false); | ||
RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); | ||
} | ||
else { | ||
var processedText = textPreprocessingIOS(result, false, false); | ||
RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNBLEPrinter.printRawData(textTo64Buffer(result, opts), function (error) { | ||
@@ -250,3 +347,7 @@ return console.warn(error); | ||
return new Promise(function (resolve, reject) { | ||
return RNNetPrinter.init(function () { return resolve(); }, function (error) { return reject(error); }); | ||
return RNNetPrinter.init(function () { | ||
return resolve(); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -256,3 +357,7 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
return RNNetPrinter.getDeviceList(function (printers) { return resolve(printers); }, function (error) { return reject(error); }); | ||
return RNNetPrinter.getDeviceList(function (printers) { | ||
return resolve(printers); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -262,3 +367,7 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
return RNNetPrinter.connectPrinter(host, port, function (printer) { return resolve(printer); }, function (error) { return reject(error); }); | ||
return RNNetPrinter.connectPrinter(host, port, function (printer) { | ||
return resolve(printer); | ||
}, function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
@@ -273,8 +382,11 @@ }, | ||
printText: function (text, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
var processedText = textPreprocessingIOS(text, false); | ||
RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); | ||
} | ||
else { | ||
var processedText = textPreprocessingIOS(text, false, false); | ||
RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNNetPrinter.printRawData(textTo64Buffer(text, opts), function (error) { | ||
@@ -286,8 +398,12 @@ return console.warn(error); | ||
printBill: function (text, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var _a, _b; | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
var processedText = textPreprocessingIOS(text); | ||
RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); | ||
} | ||
else { | ||
var processedText = textPreprocessingIOS(text, (_a = opts === null || opts === void 0 ? void 0 : opts.cut) !== null && _a !== void 0 ? _a : true, (_b = opts.beep) !== null && _b !== void 0 ? _b : true); | ||
RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNNetPrinter.printRawData(billTo64Buffer(text, opts), function (error) { | ||
@@ -298,27 +414,47 @@ return console.warn(error); | ||
}, | ||
//image url | ||
/** | ||
* image url | ||
* @param imgUrl | ||
* @param opts | ||
*/ | ||
printImage: function (imgUrl, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
RNNetPrinter.printImageData(imgUrl, opts, function (error) { return console.warn(error); }); | ||
RNNetPrinter.printImageData(imgUrl, opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNNetPrinter.printImageData(imgUrl, function (error) { | ||
return console.warn(error); | ||
}); | ||
} | ||
else { | ||
RNNetPrinter.printImageData(imgUrl, function (error) { return console.warn(error); }); | ||
} | ||
}, | ||
// base64string, except -> data:image/png;base64, | ||
/** | ||
* base64string, except -> data:image/png;base64, | ||
* @param qrCodeBase64 | ||
* @param opts | ||
*/ | ||
printQrCode: function (qrCodeBase64, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
if (Platform.OS === "ios") { | ||
RNNetPrinter.printQrCode(qrCodeBase64, opts, function (error) { return console.warn(error); }); | ||
RNNetPrinter.printQrCode(qrCodeBase64, opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNNetPrinter.printQrCode(qrCodeBase64, function (error) { | ||
return console.warn(error); | ||
}); | ||
} | ||
else { | ||
RNNetPrinter.printQrCode(qrCodeBase64, function (error) { return console.warn(error); }); | ||
} | ||
}, | ||
// android print with encoder | ||
/** | ||
* Android print with encoder | ||
* @param text | ||
*/ | ||
printRaw: function (text) { | ||
if (Platform.OS === "ios") { | ||
} | ||
else { | ||
} else { | ||
RNNetPrinter.printRawData(text, function (error) { | ||
@@ -332,12 +468,15 @@ return console.warn(error); | ||
* 80mm => 46 character | ||
* 58mm => ... | ||
* 58mm => 30 character | ||
*/ | ||
printColumnsText: function (texts, columnWidth, columnAliment, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var result = processColumnText(texts, columnWidth, columnAliment); | ||
printColumnsText: function (texts, columnWidth, columnAliment, columnStyle, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var result = processColumnText(texts, columnWidth, columnAliment, columnStyle); | ||
if (Platform.OS === "ios") { | ||
var processedText = textPreprocessingIOS(result, false); | ||
RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); | ||
} | ||
else { | ||
var processedText = textPreprocessingIOS(result, false, false); | ||
RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { | ||
return console.warn(error); | ||
}); | ||
} else { | ||
RNNetPrinter.printRawData(textTo64Buffer(result, opts), function (error) { | ||
@@ -344,0 +483,0 @@ return console.warn(error); |
import { ColumnAliment } from ".."; | ||
export declare const processColumnText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[]) => string; | ||
export declare const processColumnText: (texts: string[], columnWidth: number[], columnAliment: (ColumnAliment)[], columnStyle: string[]) => string; |
@@ -6,3 +6,3 @@ var processAlignText = function (text, restLength, align) { | ||
else if (align === 1) { | ||
return " ".repeat(Math.ceil(restLength / 2)) + text + " ".repeat(Math.floor(restLength / 2)); | ||
return " ".repeat(Math.floor(restLength / 2)) + text + " ".repeat(Math.ceil(restLength / 2)); | ||
} | ||
@@ -14,3 +14,27 @@ else if (align === 2) { | ||
}; | ||
export var processColumnText = function (texts, columnWidth, columnAliment) { | ||
var processNewLine = function (text, maxLength) { | ||
var newText; | ||
var newTextTail; | ||
var next_char = text.slice(maxLength, maxLength + 1); | ||
if (next_char === ' ') { | ||
newText = text.slice(0, maxLength); | ||
newTextTail = text.slice(maxLength, text.length); | ||
} | ||
else { | ||
var newMaxLength = text.slice(0, maxLength).split('').map(function (e) { return e; }).lastIndexOf(' '); | ||
if (newMaxLength === -1) { | ||
newText = text.slice(0, maxLength); | ||
newTextTail = text.slice(maxLength, text.length); | ||
} | ||
else { | ||
newText = text.slice(0, newMaxLength); | ||
newTextTail = text.slice(newMaxLength, text.length); | ||
} | ||
} | ||
return { | ||
text: newText, | ||
text_tail: newTextTail.trim() | ||
}; | ||
}; | ||
export var processColumnText = function (texts, columnWidth, columnAliment, columnStyle) { | ||
var new_texts = ['', '', '']; | ||
@@ -20,7 +44,8 @@ var result = ''; | ||
if (text.length >= columnWidth[idx]) { | ||
result += text.slice(0, columnWidth[idx]) + " "; | ||
new_texts[idx] = text.slice(columnWidth[idx], text.length); | ||
var processedText = processNewLine(text, columnWidth[idx]); | ||
result += (columnStyle === null || columnStyle === void 0 ? void 0 : columnStyle[idx]) + processAlignText(processedText.text, columnWidth[idx] - processedText.text.length, columnAliment[idx]) + (idx !== 2 ? " " : ""); | ||
new_texts[idx] = processedText.text_tail; | ||
} | ||
else { | ||
result += processAlignText(text.trim(), columnWidth[idx] - text.length, columnAliment[idx]); | ||
result += (columnStyle === null || columnStyle === void 0 ? void 0 : columnStyle[idx]) + processAlignText(text.trim(), columnWidth[idx] - text.length, columnAliment[idx]) + (idx !== 2 ? " " : ""); | ||
} | ||
@@ -30,5 +55,5 @@ }); | ||
if (index_nonEmpty !== -1) { | ||
result += "\n" + processColumnText(new_texts, columnWidth, columnAliment); | ||
result += "\n" + processColumnText(new_texts, columnWidth, columnAliment, columnStyle); | ||
} | ||
return result; | ||
}; |
{ | ||
"name": "react-native-thermal-receipt-printer-image-qr", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Fork of react-native-thermal-receipt-printer. A React Native Library to support USB/BLE/Net printer", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
239
README.md
@@ -11,3 +11,3 @@ # react-native-thermal-receipt-printer-image-qr | ||
<div style="display: flex; flex-direction: row; align-self: center; align-items: center"> | ||
<img src="image/receipt.jpg" alt="bill" width="250" height="580"/> | ||
<img src="image/bill.jpg" alt="bill" width="250" height="580"/> | ||
<img src="image/screenshot.jpg" alt="screenshot" width="250" height="580"/> | ||
@@ -89,238 +89,1 @@ </div> | ||
``` | ||
## Usage | ||
```javascript | ||
import { | ||
USBPrinter, | ||
NetPrinter, | ||
BLEPrinter, | ||
} from "react-native-thermal-receipt-printer-image-qr"; | ||
USBPrinter.printText("<C>sample text</C>"); | ||
USBPrinter.printBill("<C>sample bill</C>"); | ||
let orderList = [ | ||
["1. Skirt Palas Labuh Muslimah Fashion", "x2", "500$"], | ||
["2. BLOUSE ROPOL VIRAL MUSLIMAH FASHION", "x4", "500$"], | ||
["3. Women Crew Neck Button Down Ruffle Collar Loose Blouse", "x1", "3000$"], | ||
["4. Retro Buttons Up Full Sleeve Loose", "x10", "200$"], | ||
]; | ||
let columnAliment = [ColumnAliment.LEFT, ColumnAliment.CENTER, ColumnAliment.RIGHT]; | ||
let columnWidth = [46 - (7 + 12), 7, 12] | ||
for (let i in orderList) { | ||
USBPrinter.printColumnsText(orderList[i], columnWidth, columnAliment); | ||
} | ||
``` | ||
## Example | ||
### USBPrinter (only support android) | ||
```typescript | ||
interface IUSBPrinter { | ||
device_name: string; | ||
vendor_id: number; | ||
product_id: number; | ||
} | ||
``` | ||
```javascript | ||
const [printers, setPrinters] = useState([]); | ||
const [currentPrinter, setCurrentPrinter] = useState(); | ||
useEffect = () => { | ||
if(Platform.OS == 'android'){ | ||
USBPrinter.init().then(()=> { | ||
//list printers | ||
USBPrinter.getDeviceList().then(setPrinters); | ||
}) | ||
} | ||
} | ||
const _connectPrinter = (printer) => USBPrinter.connectPrinter(printer.vendorID, printer.productId).then(() => setCurrentPrinter(printer)) | ||
const printTextTest = () => { | ||
currentPrinter && USBPrinter.printText("<C>sample text</C>\n"); | ||
} | ||
const printBillTest = () => { | ||
currentPrinter && USBPrinter.printBill("<C>sample bill</C>"); | ||
} | ||
... | ||
return ( | ||
<View style={styles.container}> | ||
{ | ||
printers.map(printer => ( | ||
<TouchableOpacity key={printer.device_id} onPress={() => _connectPrinter(printer)}> | ||
{`device_name: ${printer.device_name}, device_id: ${printer.device_id}, vendor_id: ${printer.vendor_id}, product_id: ${printer.product_id}`} | ||
</TouchableOpacity> | ||
)) | ||
} | ||
<TouchableOpacity onPress={printTextTest}> | ||
<Text>Print Text</Text> | ||
</TouchableOpacity> | ||
<TouchableOpacity onPress={printBillTest}> | ||
<Text>Print Bill Text</Text> | ||
</TouchableOpacity> | ||
</View> | ||
) | ||
... | ||
``` | ||
### BLEPrinter | ||
```typescript | ||
interface IBLEPrinter { | ||
device_name: string; | ||
inner_mac_address: string; | ||
} | ||
``` | ||
```javascript | ||
const [printers, setPrinters] = useState([]); | ||
const [currentPrinter, setCurrentPrinter] = useState(); | ||
useEffect(() => { | ||
BLEPrinter.init().then(()=> { | ||
BLEPrinter.getDeviceList().then(setPrinters); | ||
}); | ||
}, []); | ||
_connectPrinter => (printer) => { | ||
//connect printer | ||
BLEPrinter.connectPrinter(printer.inner_mac_address).then( | ||
setCurrentPrinter, | ||
error => console.warn(error)) | ||
} | ||
printTextTest = () => { | ||
currentPrinter && USBPrinter.printText("<C>sample text</C>\n"); | ||
} | ||
printBillTest = () => { | ||
currentPrinter && USBPrinter.printBill("<C>sample bill</C>"); | ||
} | ||
... | ||
return ( | ||
<View style={styles.container}> | ||
{ | ||
this.state.printers.map(printer => ( | ||
<TouchableOpacity key={printer.inner_mac_address} onPress={() => _connectPrinter(printer)}> | ||
{`device_name: ${printer.device_name}, inner_mac_address: ${printer.inner_mac_address}`} | ||
</TouchableOpacity> | ||
)) | ||
} | ||
<TouchableOpacity onPress={printTextTest}> | ||
<Text>Print Text</Text> | ||
</TouchableOpacity> | ||
<TouchableOpacity onPress={printBillTest}> | ||
<Text>Print Bill Text</Text> | ||
</TouchableOpacity> | ||
</View> | ||
) | ||
... | ||
``` | ||
### NetPrinter | ||
```typescript | ||
interface INetPrinter { | ||
device_name: string; | ||
host: string; | ||
port: number; | ||
} | ||
``` | ||
_Note:_ get list device for net printers is support scanning in local ip but not recommended | ||
```javascript | ||
const EscPosEncoder = require('esc-pos-encoder') | ||
import {Buffer} from 'buffer'; | ||
componentDidMount = () => { | ||
NetPrinter.init().then(() => { | ||
this.setState(Object.assign({}, this.state, {printers: [{host: '192.168.10.241', port: 9100}]})) | ||
}) | ||
} | ||
_connectPrinter => (host, port) => { | ||
//connect printer | ||
NetPrinter.connectPrinter(host, port).then( | ||
(printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})), | ||
error => console.warn(error)) | ||
} | ||
printTextTest = () => { | ||
if (this.state.currentPrinter) { | ||
NetPrinter.printText("<C>sample text</C>\n"); | ||
NetPrinter.printImage("https://sportshub.cbsistatic.com/i/2021/04/09/9df74632-fde2-421e-bc6f-d4bf631bf8e5/one-piece-trafalgar-law-wano-anime-1246430.jpg"); | ||
let orderList = [ | ||
["1. Skirt Palas Labuh Muslimah Fashion", "x2", "500$"], | ||
["2. BLOUSE ROPOL VIRAL MUSLIMAH FASHION", "x4", "500$"], | ||
["3. Women Crew Neck Button Down Ruffle Collar Loose Blouse", "x1", "3000$"], | ||
["4. Retro Buttons Up Full Sleeve Loose", "x10", "200$"], | ||
]; | ||
let columnAliment = [ColumnAliment.LEFT, ColumnAliment.CENTER, ColumnAliment.RIGHT]; | ||
let columnWidth = [46 - (7 + 12), 7, 12] | ||
for (let i in orderList) { | ||
NetPrinter.printColumnsText(orderList[i], columnWidth, columnAliment); | ||
} | ||
NetPrinter.printQrCode("your qr base64"); | ||
NetPrinter.printBill("<C>thank you</C>\n"); | ||
} | ||
} | ||
printBillTest = () => { | ||
if(this.state.currentPrinter) { | ||
if(Platform.OS === 'ios'){ | ||
NetPrinter.printBill("<C>sample bill</C>"); | ||
} else { | ||
const encoder = new EscPosEncoder(); | ||
let _encoder = encoder | ||
.initialize() | ||
.align('center') | ||
.line('BILLING') | ||
.line('address') | ||
.line('Website: www.google.com.vn') | ||
// qr code | ||
.qrcode('hello!') | ||
.encode(); | ||
let base64String = Buffer.from(_encoder).toString('base64'); | ||
Printer.printRaw(base64String); | ||
} | ||
} | ||
}; | ||
... | ||
render() { | ||
return ( | ||
<View style={styles.container}> | ||
{ | ||
this.state.printers.map(printer => ( | ||
<TouchableOpacity key={printer.device_id} onPress={(printer) => this._connectPrinter(printer.host, printer.port)}> | ||
{`device_name: ${printer.device_name}, host: ${printer.host}, port: ${printer.port}`} | ||
</TouchableOpacity> | ||
)) | ||
} | ||
<TouchableOpacity onPress={() => this.printTextTest()}> | ||
<Text> Print Text </Text> | ||
</TouchableOpacity> | ||
<TouchableOpacity onPress={() => this.printBillTest()}> | ||
<Text> Print Bill Text </Text> | ||
</TouchableOpacity> | ||
</View> | ||
) | ||
}; | ||
... | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7008431
154
1498
88