2

I am using node-thermal-printer package from github ( https://github.com/Klemen1337/node-thermal-printer ) in order to print on Esc-pos printer.

When I try to print, I get error : Unhandled Rejection (TypeError): Net.connect is not a function

I searched a lot and there was only one unanswered question like mine here: https://gitter.im/Klemen1337/node-thermal-printer?at=588fbd82c0f28dd862629227

I am trying to print from Reactjs project using Chrome on Mac, I tried it on Android device too but I get the same issue.

I installed all the dependencies like : net, write-file-queue, unorm, iconv-file too.

This is the print image code:

  testPrint = async () => {
    const ThermalPrinter = require("../../../node_modules/node-thermal-printer")
        .printer;
    const Types = require("../../../node_modules/node-thermal-printer").types;
    let printer = new ThermalPrinter({
        type: Types.EPSON,
        interface: "tcp://192.168.0.100:9100"
    });

    printer.alignCenter();
    printer.println("Hello world");

    try {
        let execute = printer.execute();
        console.error("Print done!");
    } catch (error) {
        console.log("Print failed:", error);
    }
 };

and the problem happens in "var printer = Net.connect(" copied from network.js file of the package as below:

async execute(buffer) {
return new Promise((resolve, reject) => {
  let name = this.host + ":" + this.port;
  var printer = Net.connect(
    {
      host: this.host,
      port: this.port,
      timeout: this.timeout
    },
    function () {
      printer.write(buffer, null, function () {
        resolve("Data sent to printer: " + name);
        printer.destroy();
      });
    }
  );

  printer.on('error', function (error) {
    reject(error);
    printer.destroy();
  });

  printer.on('timeout', function () {
    reject(new Error("Socket timeout"));
    printer.destroy();
  });
});
}}

Any advice would be appreciated.

albert sh
  • 1,095
  • 2
  • 14
  • 31

3 Answers3

2

You can create a local middleware service in node using express that calls ThermalPrinter

const express = require('express')
const bodyParser = require('body-parser');
const cors = require('cors');
const ThermalPrinter = require("node-thermal-printer").printer;
const Types = require("node-thermal-printer").types;

const app = express();
const port = 3005;

app.use(cors());

// Configuring body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/printThermal', async (req, res) => {
    const value = req.body;
    let printer = new ThermalPrinter({
        type: Types.EPSON,  // 'star' or 'epson'
        interface: "tcp://127.0.0.1:9100",
        options: {
            timeout: 1000
        },
        width: 48,                         // Number of characters in one line - default: 48
        characterSet: 'SLOVENIA',          // Character set - default: SLOVENIA
        removeSpecialCharacters: false,    // Removes special characters - default: false
        lineCharacter: "-",                // Use custom character for drawing lines - default: -
    });

    let isConnected = await printer.isPrinterConnected();
    console.log("Printer connected:", isConnected);

    printer.alignCenter();
    //await printer.printImage('./assets/olaii-logo-black-small.png');

    printer.alignLeft();
    printer.newLine();
    printer.println("Hello World!");

    try {
        await printer.execute();
        console.log("Print success.");
    } catch (error) {
        console.error("Print error:", error);
    }
});

app.listen(port, () => console.log(`Hello world app listening on port ${port}!`));

Then you can call it from your react app :

export const printThermal = async (text) => {
  const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title: 'React POST Request Example' })
  };
  fetch('http://localhost:3005/printThermal', requestOptions)
    .then(response => response.json())
}
1

It seems that this package is not designed to use from browser.

https://github.com/Klemen1337/node-thermal-printer/issues/142

albert sh
  • 1,095
  • 2
  • 14
  • 31
0

This is not an escpos question. Please be more precise when categorizing your questions.

Marc Balmer
  • 1,780
  • 1
  • 11
  • 18