简体   繁体   中英

How to print with a ESC/POS printer starting from javascript (no interface)

I am trying to print a receipt from my electron app that uses Javascript.

I know that javascipt doesn't allow printing from client side so I have to use a third party language or programme to print. I tried Qz Tray 2.0 but then I faced a probleme with the certeficate so now I am trying to find another way:

For the moment what I need to do is simple : passe data to a chosen printer installed on windows that will use the correct driver.

EDIT :

I have multiple printers, connected on the network, so I need to get there IP adresse first before I can do anything. 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.

Any ideas are welcome, thank you

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM