-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
How to print to an specific printer by name? #94
Comments
Hello @juancarlosgzs! I'm here to help you with your issue. I'm a bot designed to assist you with bug fixes, answer your questions, and guide you on becoming a contributor. Let's work together to solve the problem you're facing. To print to a specific printer by name using Electron and handle all printers connected to the computer (USB or Bluetooth), you can use the following approach: const { app, BrowserWindow } = require('electron');
const { Bluetooth, Printer } = require('..'); // Adjust the path as necessary
app.on('ready', async () => {
const mainWindow = new BrowserWindow();
try {
const availablePrinters = await mainWindow.webContents.getPrintersAsync();
const preferredPrinterName = 'Your_Printer_Name'; // Replace with your printer's name
const selectedPrinter = availablePrinters.find(printer => printer.name === preferredPrinterName);
if (selectedPrinter) {
console.log('Connect to ' + selectedPrinter.name);
const device = await Bluetooth.getDevice(selectedPrinter.address, selectedPrinter.channel);
const printer = await Printer.create(device);
await printer.text('hello');
await printer.cut();
await printer.close();
console.log('print job done');
} else {
console.log('Printer not found');
}
} catch (error) {
console.log('error', error);
}
}); This script will:
Make sure to replace |
Any ideas? Still can't find a way |
To print to a specific thermal printer by name using Electron, ensuring compatibility with both USB and Bluetooth printers, you can use the ESC/POS Printer driver for Node.js. Below are example code snippets that demonstrate how to connect to both Bluetooth and USB printers by name. Bluetooth Printer Exampleconst { Bluetooth, Printer } = require('escpos-printer-driver');
(async () => {
try {
const availablePrinters = await Bluetooth.findPrinters();
const preferredPrinterName = 'Your_Printer_Name'; // Replace with your printer's name
const btPrinter = availablePrinters.filter(p => p && p.name === preferredPrinterName)[0];
if (!btPrinter) {
throw new Error(`Printer with name ${preferredPrinterName} not found`);
}
console.log('Connect to ' + btPrinter.name);
const device = await Bluetooth.getDevice(btPrinter.address, btPrinter.channel);
const printer = await Printer.create(device);
await printer.text('hello');
await printer.cut();
await printer.close();
console.log('print job done');
} catch (error) {
console.log('error', error);
}
})(); USB Printer Exampleconst { USB, Printer } = require('escpos-printer-driver');
(async () => {
try {
const device = await USB.getDevice();
const printer = await Printer.create(device);
await printer.text('hello');
await printer.cut();
await printer.close();
console.log('print job done');
} catch (error) {
console.log('error', error);
}
})(); For both examples, you need to replace |
I'm trying to print to an specific thermal printer but I just have the data that is returned by the getPrintersAsync function from Electron. In the past with other libraries, I used the name itself of the printer in order to print to an specific printer, how can I do that with this library?
In my use case I need to handle all printers that are connected to the computer, either for it to be usb or bluetooth.
@dohooo
The text was updated successfully, but these errors were encountered: