Skip to content

Unable to get printer interface #268

@amrutakawale

Description

@amrutakawale

I have implemented this library for the STAR TSP100III printer, but code is not working as I am using printer which is connected by USB.
Code is broken at this line: **interface: tcp://${printerInfo.name}, // Printer interface (assuming network interface here) **

ipcMain.on("printOrderInfo", (event, count, printContent, isCashOrCheck) => {

// Get available printers

const printers = mainWindow.webContents.getPrinters();

// Find the printer named "Star TSP100 Cutter"

let printerInfo = printers.find((printer) =>

printer.name.includes("Star TSP100 Cutter")

);

// Check if the printer is available

if (!printerInfo) {

console.log("Star TSP100 Cutter Printer is not available for Print.");

return;

}

// Initialize the printer instance

const printer = new ThermalPrinter({

type: PrinterTypes.STAR,  // Printer type: 'star' or 'epson'

interface: `tcp://${printerInfo.name}`,  // Printer interface (assuming network interface here) 

characterSet: CharacterSet.PC852_LATIN2,  // Printer character set

removeSpecialCharacters: false,  // Do not remove special characters by default

lineCharacter: "=",  // Character for lines in the receipt (default: "-")

breakLine: BreakLine.WORD,  // Break line after word (default: WORD)

options: {

  preview: false,  // No preview

  width: "240px",  // Set content body width

  margin: "0 0 0 0",  // Set margin of content body

  copies: 1,  // Number of copies to print

  printerName: printerInfo.name,  // Printer name from the available printers list

  timeOutPerLine: 400,  // Timeout per line for printing

  silent: true,  // Do not show preview or print dialogs

  timeout: 5000  // Timeout for connection (network printers)

}

});

// Loop through each print request and print

for (let index = 0; index < count; index++) {

const data = [

  {

    type: "text",  // Type of data: text

    value: printContent[index],  // Text content to print

    css: {

      "text-align": "center",  // Center align the text

    },

  }

];

// Add the raw command to open the cash drawer if `isCashOrCheck` is true

if (isCashOrCheck) {

  // Raw command for cash drawer (e.g., BEL \x07)

  data.push({

    type: "raw",  // Use 'raw' type for sending control commands

    value: "\x07",  // ASCII BEL (bell) to open cash drawer

  });

}

try {

  // Print the text data

  await printer.printText(data);  // Print the content as text

  await printer.cut();  // Cut the paper after printing

  // If the transaction is cash or check, print the raw command to open the drawer

  if (isCashOrCheck) {

    const rawCommand = Buffer.from([0x07]);  // Convert \x07 to a buffer for raw printing

    await printer.printRaw(rawCommand);  // Send the raw command

  }

} catch (error) {

  // Handle printing errors

  console.error("Failed to print. Error: " + error);

}

}

});

Please help me to resolve it.
Thank you in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions