diff --git a/README.md b/README.md index acaf1b4..9123a5d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ -# thermal_printer +# Note: +This is a forked project of: https://pub.dev/packages/thermal_printer +This has an additional function to get default printer selected in windows in case of USB with function getDefaultPrinter() in dart -[![Pub Version](https://img.shields.io/badge/pub-v1.0.5-green)](https://pub.dev/packages/thermal_printer) +# Printer_service + + A library to discover printers, and send printer commands. diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 17e411f..f6ea60f 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -6,7 +6,7 @@ description: Demonstrates how to use the thermal_printer plugin. publish_to: 'none' # Remove this line if you wish to publish to pub.dev environment: - sdk: '>=2.18.4 <3.3.10' + sdk: '>=2.18.4 <3.5.9' # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions diff --git a/lib/printer.dart b/lib/printer.dart index c3e4b15..7c84bc7 100644 --- a/lib/printer.dart +++ b/lib/printer.dart @@ -27,6 +27,7 @@ abstract class PrinterConnector { Future send(List bytes); Future connect(T model); Future disconnect({int? delayMs}); + Future getDefaultPrinter(); } abstract class GenericPrinter extends Printer { diff --git a/lib/src/connectors/bluetooth.dart b/lib/src/connectors/bluetooth.dart index 7b4808b..47b1da8 100644 --- a/lib/src/connectors/bluetooth.dart +++ b/lib/src/connectors/bluetooth.dart @@ -260,4 +260,10 @@ class BluetoothPrinterConnector implements PrinterConnector getDefaultPrinter() { + // TODO: implement getDefaultPrinter + throw UnimplementedError(); + } } diff --git a/lib/src/connectors/tcp.dart b/lib/src/connectors/tcp.dart index c33532c..e38f308 100644 --- a/lib/src/connectors/tcp.dart +++ b/lib/src/connectors/tcp.dart @@ -186,4 +186,10 @@ class TcpPrinterConnector implements PrinterConnector { }, ); } + + @override + Future getDefaultPrinter() { + // TODO: implement getDefaultPrinter + throw UnimplementedError(); + } } diff --git a/lib/src/connectors/usb.dart b/lib/src/connectors/usb.dart index 44312ac..d5a62c4 100644 --- a/lib/src/connectors/usb.dart +++ b/lib/src/connectors/usb.dart @@ -198,4 +198,16 @@ class UsbPrinterConnector implements PrinterConnector { else return false; } + + @override + Future getDefaultPrinter() async{ + try{ + var printerName = await flutterPrinterChannel.invokeMethod('getDefaultPrinter'); + return printerName; + } catch(e){ + return ""; + } + } + + } diff --git a/lib/src/printer_manager.dart b/lib/src/printer_manager.dart index a149b50..a50625b 100644 --- a/lib/src/printer_manager.dart +++ b/lib/src/printer_manager.dart @@ -25,6 +25,16 @@ class PrinterManager { } } + Future getDefaultPrinter({PrinterType type = PrinterType.usb}) async { + if (type == PrinterType.bluetooth && (Platform.isIOS || Platform.isAndroid)) { + return await bluetoothPrinterConnector.getDefaultPrinter(); + } else if (type == PrinterType.usb && (Platform.isAndroid || Platform.isWindows)) { + return await usbPrinterConnector.getDefaultPrinter(); + } else { + return await tcpPrinterConnector.getDefaultPrinter(); + } + } + Future connect({required PrinterType type, required BasePrinterInput model}) async { if (type == PrinterType.bluetooth && (Platform.isIOS || Platform.isAndroid)) { try { diff --git a/pubspec.yaml b/pubspec.yaml index 75bf575..ec7fae4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ -name: thermal_printer +name: printer_service description: A flutter plugin that prints esc commands to printers in different platforms such as android, ios, windows and different interfaces Bluetooth and BLE, TCP and USB version: 1.0.5 -homepage: https://github.com/codingdevs/thermal_printer +homepage: https://github.com/arpitsharma007/printer_service/ # This package supports all platforms listed below. platforms: diff --git a/windows/thermal_printer_plugin.cpp b/windows/thermal_printer_plugin.cpp index fbaa30a..11d3b97 100644 --- a/windows/thermal_printer_plugin.cpp +++ b/windows/thermal_printer_plugin.cpp @@ -125,6 +125,28 @@ namespace return result->Success(EncodableValue(success)); } } + else if (method_call.method_name().compare("getDefaultPrinter") == 0) + { + wchar_t defaultPrinterName[MAX_PATH]; // Corrected to wchar_t buffer + + DWORD size = MAX_PATH; + if (GetDefaultPrinter(defaultPrinterName, &size) > 0) { // Pass the buffer directly + wprintf(L"Default Printer: %s\n", defaultPrinterName); // Use wprintf for wide strings + int utf8Length = WideCharToMultiByte(CP_UTF8, 0, defaultPrinterName, -1, NULL, 0, NULL, NULL); + if (utf8Length > 0) { + std::string utf8PrinterName(utf8Length, 0); + WideCharToMultiByte(CP_UTF8, 0, defaultPrinterName, -1, &utf8PrinterName[0], utf8Length, NULL, NULL); + + // Return the UTF-8 encoded printer name + return result->Success(EncodableValue(utf8PrinterName)); + } else { + return result->Error("CONVERSION_ERROR", "Failed to convert printer name to UTF-8."); + } + } else { + printf("Error getting default printer\n"); + } + + } else { result->NotImplemented();