使用Electron app进行原始打印

时间:2017-10-08 23:48:27

标签: javascript printing electron network-printers epos

我想创建一个支持原始打印的电子应用程序。

关于我可以采取的图书馆或路径的任何建议将不胜感激。我做了一些研究,但似乎没有什么是最新的。我想获得所有可用的打印机并获得默认打印机并使用它进行打印。

我有一个小例子,你可以告诉我,它会很棒!

2 个答案:

答案 0 :(得分:0)

您应该查看.getPrinters().print()方法,它们似乎适合您的使用案例。

答案 1 :(得分:0)

尝试了几种方法和程序包后,我能够通过以下方式获得成功:

  • 共享打印机
  • 使用node-cmd将文件副本发送到共享打印机。

注意,我只在Windows上使用和测试过。

yarn add node-cmd

示例

const fs = window.require('fs')
const path = window.require('path')
const cmd = window.require('node-cmd')

//Save the raw output to the filesystem
const filePath = path.join(__dirname, 'rawprint.prn') //or wherever you want to save it

//Create a command to copy the file to the shared printer path (e.g. \\localhost\DPD ). Make sure that var is sanitised first! 
const command = `COPY /B "${filePath}" "${pathToSharedPrinter}"`

cmd.get( command, (err, data, stderr) => {

    if ( !err ) {
        console.log('Success!')
    } else {
        console.log( err.message )
    }

})