Skip to content

Commit

Permalink
Merge pull request #63 from LavaSean/feat/support-other-language-prin…
Browse files Browse the repository at this point in the history
…ting

feat/support-other-language-printing
  • Loading branch information
AllInOneYT authored Aug 21, 2024
2 parents 39073ab + bfdf31a commit 226ab69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.bumptech.glide.Glide;
import com.dantsu.escposprinter.EscPosPrinter;
import com.dantsu.escposprinter.EscPosCharsetEncoding;
import com.dantsu.escposprinter.connection.DeviceConnection;
import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections;
import com.dantsu.escposprinter.connection.tcp.TcpConnection;
Expand Down Expand Up @@ -63,7 +64,7 @@ public String getName() {
}

@ReactMethod
public void printTcp(String ipAddress, double port, String payload, boolean autoCut, boolean openCashbox, double mmFeedPaper, double printerDpi, double printerWidthMM, double printerNbrCharactersPerLine, double timeout, Promise promise) {
public void printTcp(String ipAddress, double port, String payload, boolean autoCut, boolean openCashbox, double mmFeedPaper, double printerDpi, double printerWidthMM, double printerNbrCharactersPerLine, double timeout, String encoding, int charsetId, Promise promise) {
//
// 05-05-2021
// https://reactnative.dev/docs/native-modules-android
Expand All @@ -77,14 +78,14 @@ public void printTcp(String ipAddress, double port, String payload, boolean auto
this.jsPromise = promise;
try {
TcpConnection connection = new TcpConnection(ipAddress, (int) port, (int) timeout);
this.printIt(connection, payload, autoCut, openCashbox, mmFeedPaper, printerDpi, printerWidthMM, printerNbrCharactersPerLine);
this.printIt(connection, payload, autoCut, openCashbox, mmFeedPaper, printerDpi, printerWidthMM, printerNbrCharactersPerLine, encoding, charsetId);
} catch (Exception e) {
this.jsPromise.reject("Connection Error", e.getMessage());
}
}

@ReactMethod
public void printBluetooth(String macAddress, String payload, boolean autoCut, boolean openCashbox, double mmFeedPaper, double printerDpi, double printerWidthMM, double printerNbrCharactersPerLine, Promise promise) {
public void printBluetooth(String macAddress, String payload, boolean autoCut, boolean openCashbox, double mmFeedPaper, double printerDpi, double printerWidthMM, double printerNbrCharactersPerLine,String encoding, int charsetId, Promise promise) {
this.jsPromise = promise;
BluetoothConnection btPrinter;

Expand All @@ -108,7 +109,7 @@ public void printBluetooth(String macAddress, String payload, boolean autoCut, b
ActivityCompat.requestPermissions(getCurrentActivity(), new String[]{Manifest.permission.BLUETOOTH_SCAN}, 1);
} else {
try {
this.printIt(btPrinter.connect(), payload, autoCut, openCashbox, mmFeedPaper, printerDpi, printerWidthMM, printerNbrCharactersPerLine);
this.printIt(btPrinter.connect(), payload, autoCut, openCashbox, mmFeedPaper, printerDpi, printerWidthMM, printerNbrCharactersPerLine, encoding, charsetId);
} catch (Exception e) {
this.jsPromise.reject("Connection Error", e.getMessage());
}
Expand Down Expand Up @@ -187,9 +188,11 @@ private String preprocessImgTag(EscPosPrinter printer, String text) {
return sb.toString();
}

private void printIt(DeviceConnection printerConnection, String payload, boolean autoCut, boolean openCashbox, double mmFeedPaper, double printerDpi, double printerWidthMM, double printerNbrCharactersPerLine) {
private void printIt(DeviceConnection printerConnection, String payload, boolean autoCut, boolean openCashbox, double mmFeedPaper, double printerDpi, double printerWidthMM, double printerNbrCharactersPerLine, String encoding,
int charsetId) {
try {
EscPosPrinter printer = new EscPosPrinter(printerConnection, (int) printerDpi, (float) printerWidthMM, (int) printerNbrCharactersPerLine);
EscPosCharsetEncoding charsetEncoding = new EscPosCharsetEncoding(encoding, charsetId);
EscPosPrinter printer = new EscPosPrinter(printerConnection, (int) printerDpi, (float) printerWidthMM, (int) printerNbrCharactersPerLine,charsetEncoding);
String processedPayload = preprocessImgTag(printer, payload);

if (openCashbox) {
Expand Down
18 changes: 15 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ type NativeModuleType = typeof NativeModules & {
printerDpi: number,
printerWidthMM: number,
printerNbrCharactersPerLine: number,
timeout: number
timeout: number,
encoding: string,
charsetId: number,
): Promise<void>;
printBluetooth(
macAddress: string,
Expand All @@ -27,7 +29,9 @@ type NativeModuleType = typeof NativeModules & {
mmFeedPaper: number,
printerDpi: number,
printerWidthMM: number,
printerNbrCharactersPerLine: number
printerNbrCharactersPerLine: number,
encoding: string,
charsetId: number,
): Promise<void>;
getBluetoothDeviceList(): Promise<BluetoothPrinter[]>;
};
Expand All @@ -50,6 +54,8 @@ interface PrintTcpInterface extends PrinterInterface {
ip: string;
port: number;
timeout: number;
encoding: string;
charsetId: number;
}

interface PrintBluetoothInterface extends PrinterInterface {
Expand All @@ -68,6 +74,8 @@ let defaultConfig: PrintTcpInterface & PrintBluetoothInterface = {
printerWidthMM: 80,
printerNbrCharactersPerLine: 42,
timeout: 30000,
encoding: 'UTF-8',
charsetId: 0,
};

const getConfig = (
Expand All @@ -90,6 +98,8 @@ const printTcp = async (
printerWidthMM,
printerNbrCharactersPerLine,
timeout,
encoding,
charsetId,
} = getConfig(args);

await ThermalPrinterModule.printTcp(
Expand All @@ -102,7 +112,9 @@ const printTcp = async (
printerDpi,
printerWidthMM,
printerNbrCharactersPerLine,
timeout
timeout,
encoding,
charsetId,
);
};

Expand Down

0 comments on commit 226ab69

Please sign in to comment.