【问题标题】:Print from an Electron application从 Electron 应用程序打印
【发布时间】:2015-11-23 07:19:19
【问题描述】:

我正在尝试使用 Electron 应用程序中的 node printer,但一旦我添加行以使用打印机,应用程序就会崩溃。

控制台输出如下:

[1]    9860 segmentation fault (core dumped)  node_modules/electron-prebuilt/dist/electron.

这是我正在运行的应用程序:

var app = require('app');
var BrowserWindow = require('browser-window');
var printer = require('printer');

require('crash-reporter').start();

app.on('ready', function() {
  var mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadUrl('file://' + __dirname + '/app/index.html');

  mainWindow.openDevTools();

  printer.printDirect({data:"print from Node.JS buffer" // or simple String: "some text"
      , printer:'HP-Deskjet-F4400-series' // printer name, if missing then will print to default printer
      , type: 'TEXT' // type: RAW, TEXT, PDF, JPEG, .. depends on platform
      , success:function(jobID){
          console.log("sent to printer with ID: "+jobID);
      }
      , error:function(err){console.log(err);}
  });      
});

我错过了什么吗?

我自己尝试了节点打印机,我成功打印了一些乱码。

【问题讨论】:

    标签: javascript node.js printing electron


    【解决方案1】:

    node-printer 使用本机绑定并根据docs

    Electron 支持原生 Node 模块,但自从 Electron 正在使用与官方 Node 不同的 V8 版本,您必须 构建时手动指定 Electron 头文件的位置 本机模块。

    我想这就是您收到seg fault 的原因。尝试根据文档中提到的电子标头构建模块:

    npm install --save-dev electron-rebuild
    
    # Every time you run npm install, run this too
    ./node_modules/.bin/electron-rebuild
    

    【讨论】:

    • 没有成功。这就是现在发生的事情:npm ERR! Failed at the printer@0.1.1 install script 'node-gyp rebuild'. npm ERR! This is most likely a problem with the printer package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node-gyp rebuild npm ERR! You can get their info via: npm ERR! npm owner ls printer npm ERR! There is likely additional logging output above.
    • @leamasuero 查看文档,还有另一种使用 node-gyp 的方法。也许这有帮助。
    • @leamasuero,上次我遇到这个错误,我通过将我的节点版本降级到8.x.x来解决它。
    【解决方案2】:
    app.on('ready', () => {
      let win = new BrowserWindow({width: 800, height: 600, resizable: false})
      win.loadURL('file://' + __dirname + '/index.html')
      win.webContents.on('did-finish-load', () => {
        win.webContents.printToPDF({marginsType: 2, pageSize: "A3", landscape: false}, (error, data) => {
          if (error) throw error
          fs.writeFile('output.pdf', data, (error) => {
    
            //getTitle of Window
            console.log(win.webContents.getTitle())
    
            //Silent Print
    
            if (error) throw error
            console.log('Write PDF successfully.')
          })
        })
      })
    })
    

    否则你也可以使用下面这行

    win.webContents.print({silent:true, printBackground:true})
    

    【讨论】:

    • writeFile('output.pdf'...) 不工作,但底部的工作!
    【解决方案3】:

    node-printer 模块中包含 C++ 代码。这意味着您必须使用与 electron 使用的相同版本的节点来编译它。这实际上是可行的,但它相当复杂。

    另一方面,Electron 已经有了打印 API:

    https://electronjs.org/docs/api/web-contents#contentsprintoptions-callback

    如果此 api 还不够,并且您仍想利用 node-printer 模块,请告诉我,我将编辑此回复,并提供有关如何分叉和修复 node-printer 的更长答案,使其与电子兼容。

    【讨论】:

    • 拥有与电子兼容的node-printer 需要将其移植到NAN 2,对吗?
    • 我的意思是 Electron 现在使用 node v4 并且 V8 api 改变了很多。
    • 是的,我想是的。我在使用不同的包时遇到了类似的情况,我不得不分叉它以实现我需要的更改,以便使用电子构建它。大多数变化都是肤浅的......其中大部分。
    • 节点打印机在 Windows 上不适合我。仅当我想打印简单的文本或一些 RAW 数据时才可用。 Buy 无法打印任何 JPEG 或 PDF。你在windows上测试过吗?
    • 传说中的胡须顺便说一句
    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多