Klemen1337 / node-thermal-printer

Node.js module for Epson, Star, Tanca, Drauma and Brother thermal printers command line printing.
MIT License
764 stars 243 forks source link

Node Thermal with VueJs Integration #88

Closed igorbag closed 5 years ago

igorbag commented 5 years ago

Hello everyone.

Is it possible to integrate this lib with the VueJs?

Has anyone used anything related?

kozmoz commented 5 years ago

It needs to run on Node, so it can access the printer. It is not running in the browser, like Vue.js is.

I use AngularJS as the front-end layer within an Electron app (the front-end part runs in the Chrome layer). node-thermal-printer is running in the Node proces of the app and the frontend communicates with it using events.

https://electronjs.org

igorbag commented 5 years ago

Hello, @kozmoz!!

thanks for help.

In my case, i use ElectronJs and VueJs to Front-end layer.

Could you tell me how to integrate the printer with the Electron and later how to use it in VueJs or Angular?

kozmoz commented 5 years ago

From the frontend you can use the electron.ipcRenderer to send messages to the Node process. The pinter library lives in the Node layer. In short:

Front-end JS (Angular or Vue):

electron = require('electron');
electron.ipcRenderer.send('printOrder', ...args);

And in the main.js file (Node):

const {ipcMain} = require('electron');
ipcMain.on('printOrder', (event, order) => {
    printServices.printOrder(order)
        .then(() => event.sender.send(`printOrderSuccessResponse`, true))
        .catch(error => event.sender.send(`printOrderErrorResponse`, error));
});

The printService looks like this:

const printer = require('node-thermal-printer');
const printerConfig = {
    type: printer.printerTypes.EPSON,
    interface: `tcp://${config.printerIp}:` + (config.printerPort || 9100),
};
printer.init(printerConfig);
printer.println(' Printer successfully configured ');
printer.execute();

I left out the code for the Promises and also didn't include the listeners for the "printOrderSuccessResponse" to detect if printing was successful, but I think you will get the idea.

Good luck!

igorbag commented 5 years ago

Awesome @kozmoz , thanks a lot for your help.