Skip to content

Default printer function added #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

<!-- [![Pub Version](https://img.shields.io/badge/pub-v1.0.5-green)](https://pub.dev/packages/thermal_printer) -->

A library to discover printers, and send printer commands.

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class PrinterConnector<T> {
Future<bool> send(List<int> bytes);
Future<bool> connect(T model);
Future<bool> disconnect({int? delayMs});
Future<String> getDefaultPrinter();
}

abstract class GenericPrinter<T> extends Printer {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/connectors/bluetooth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,10 @@ class BluetoothPrinterConnector implements PrinterConnector<BluetoothPrinterInpu
return false;
}
}

@override
Future<String> getDefaultPrinter() {
// TODO: implement getDefaultPrinter
throw UnimplementedError();
}
}
6 changes: 6 additions & 0 deletions lib/src/connectors/tcp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,10 @@ class TcpPrinterConnector implements PrinterConnector<TcpPrinterInput> {
},
);
}

@override
Future<String> getDefaultPrinter() {
// TODO: implement getDefaultPrinter
throw UnimplementedError();
}
}
12 changes: 12 additions & 0 deletions lib/src/connectors/usb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,16 @@ class UsbPrinterConnector implements PrinterConnector<UsbPrinterInput> {
else
return false;
}

@override
Future<String> getDefaultPrinter() async{
try{
var printerName = await flutterPrinterChannel.invokeMethod('getDefaultPrinter');
return printerName;
} catch(e){
return "";
}
}


}
10 changes: 10 additions & 0 deletions lib/src/printer_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class PrinterManager {
}
}

Future<String> 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<bool> connect({required PrinterType type, required BasePrinterInput model}) async {
if (type == PrinterType.bluetooth && (Platform.isIOS || Platform.isAndroid)) {
try {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
22 changes: 22 additions & 0 deletions windows/thermal_printer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down