react-thermal-printer icon indicating copy to clipboard operation
react-thermal-printer copied to clipboard

Unable to connect printer using usb or serial requestPort

Open abhishek73magar opened this issue 1 year ago • 4 comments

Unable to connect and print the text using usb cable and also get error :

DOMException: Failed to execute 'open' on 'SerialPort': Failed to open serial port. : (this in chrome)

TypeError: Cannot read properties of undefined (reading 'requestPort') at handle (App.js:20:1) : (this in brave browser)

is it automatic connect to the printer or somthing else can you tellme how should i fixed it

import "./App.css";
import "leaflet/dist/leaflet.css";
import "leaflet-draw/dist/leaflet.draw.css";
import { Printer, Text, render } from "react-thermal-printer";


function App() {
  const handle = async() => {
    try {
      const data = await render(
        <Printer type="epson">
          <Text>Hello World</Text>
        </Printer>
      );
      
      const port = await window.navigator.serial.requestPort();
      await port.open({ baudRate: 9600 });
      
      const writer = port.writable?.getWriter();
      if (writer != null) {
        await writer.write(data);
        writer.releaseLock();
      }
      
     console.log('ehy', data, port)
    } catch (error) {
      console.log(error)
    }
  }

  return (
    <div className="App">
      <button onClick={handle}>Print</button>
    </div>
  );
}

export default App;

abhishek73magar avatar Jul 28 '23 04:07 abhishek73magar

window.navigator.serial and window.navigator.usb are both in experimental and are not supported by all browsers, in fact it is only supported in the latest versions of chrome, edge and opera.

I am also facing this issues and still looking for a better way that is cross browser to connect to thermal printer that are attached through USB.

@seokju-na any suggestions pleas.

Hassan-jr avatar Aug 01 '23 08:08 Hassan-jr

Since react-thermal-printer library focus rendering the content marked up with JSX as data that can be printed on a thermal printer, but not responsible for the interface sending data to the thermal printer.

This library won't solve connecting to the printer, but i can give some guide.

In Web Browser:

In Electron or Node.js:

seokju-na avatar Aug 06 '23 07:08 seokju-na

window.navigator.serial and window.navigator.usb are both in experimental and are not supported by all browsers, in fact it is only supported in the latest versions of chrome, edge and opera.

I am also facing this issues and still looking for a better way that is cross browser to connect to thermal printer that are attached through USB.

@seokju-na any suggestions pleas.

Have you found a solution? I'm facing the same problem with a thermal printer attached via USB and not serial. The WebUSB API could help but it's a bit intricate to work, so using the library with USB would be great but unfortunately, I'm missing the serial port on my PC so I can't use it as of now.

Toscanah avatar Apr 22 '24 22:04 Toscanah

I found this: https://github.com/drffej/webusb.printer

It's a very simple snippet that actually works and lets you print some text on a USB-attached printer (just adjust the vendorId filter to your case). You can take the code as an example and print your own stuff.

Also, if you see an error "Failed to execute 'open' on 'USBDevice': Access denied." install this "Zadig" as explained here: https://stackoverflow.com/a/76488056/19193089

You won't have any cool looking Components like this library offers, you'll have to format your own string and feed it to the encoder, but it should work.

Toscanah avatar Apr 22 '24 23:04 Toscanah