0

I know that JavaScript doesn't allow printing from client side so I have to use a third party language or program to print. I tried Qz Tray 2.0 but then I faced a problem with the certificate, so now I am trying to find another way.

I need to send data to a chosen printer installed on Windows that will use the correct driver.

I have multiple printers, connected on the network, so I need to get the IP adress first. Electron doesn't offer an options for this (I can only get the name and driver but not the IP), but as last resort I can make so the user inputs the IP.

So, how can I print something on a ESC/POS printer on Windows?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
  • Try this package. https://stackoverflow.com/a/73864877/15318755 I hope it helps you. – jkim Sep 30 '22 at 00:58

2 Answers2

0

Depends on what you're trying to print exactly, but you could try electron print.

You could see if it works in your system with a quick "helloworld" snippet:

const printer = require('electron-print');
app.on('ready', function() {
    printer.print("Text sent to printer.")
});

Another thing you could try is using electron's PrinterInfo[] object. You can get a JSON array with the printers as follows:

contents.getPrinters()

For each printer it will return an "options" configuration object (docs) which you can use to print using:

contents.print([options])

That would print the current webpage. You might even be able to do silent printing by opening the print contents into a hidden window and using:

webContents.print({silent: true, printBackground: false, deviceName: ''})

Really depends on what you want to do. There's a bunch of config params you can play with.

EDIT AFTER COMMENT:

Seems like electron-print wraps node-print, which supports sending printing commands directly to the printer using printDirect(), this comes from their raw printing example:

cmds = 'your printer commands, guess you have the printer spec'
printer.printDirect({data: cmds
    , type: 'RAW'
    , success:function(jobID: any){
        console.log("sent to printer with ID: "+jobID);
     }
    , error:function(err: any){console.log(err);}
});

Not sure if electron-print provides a wrapper for this function too, but you could easily add it yourself if you think it's worth a try.

EDIT 2:

Just found this: https://www.neodynamic.com/articles/How-to-print-raw-ESC-POS-commands-from-Javascript

DanyAlejandro
  • 1,440
  • 13
  • 24
  • Well ... I said ESC/POS data, so I need to send RAW data to the device, and using : webContents.print is not a solution :( – Med-Amine Benyettou Jul 14 '19 at 18:55
  • Here's the result : the first edit : I can't select multiple printers only default one. second edit : That uses a user interface that "works" but asks the user for permission each time for print :( – Med-Amine Benyettou Jul 14 '19 at 21:05
0

Well, this question isn't explained well. The first answer should have been the answer to the article, but in case you need another solution you could use Windows or any OS command line and run them like this:

On Windows make a .bat file in your app root

shell.openItem(fullPath)

Using this command you can run the bat file remember to Import shell from 'electron' or 'remote'

And a guide to printing from the command line (even though I haven't read it)

James Tomasino
  • 3,520
  • 1
  • 20
  • 38
Emad Fani
  • 415
  • 4
  • 13
  • I don't realy know how to use .bat files, but in my case, I have to get the printer IP and then print it and it's not just 1 printer but many, and the user can change the printers. – Med-Amine Benyettou Jul 14 '19 at 21:08