从电子应用程序打印 [英] Print from an Electron application

查看:65
本文介绍了从电子应用程序打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Electron应用程序中使用节点打印机,但是我很快添加行以使用打印机,应用程序将崩溃。

I'm trying to use node printer from an Electron application, but as soon I add the lines to use the printer, the app crashes.

控制台输出以下内容:

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

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

This is the app I'm running:

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);}
  });      
});

我错过了什么吗?

I独自尝试了节点打印机,然后我成功打印了一些乱码。

I tried the node printer on its own, and I successfully printed some gibberish text.

推荐答案

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

node-printer uses native bindings and according to the docs:


Electron支持本机节点模块,但是由于Electron
使用的是其他V8版本从官方节点开始,在构建
本机模块时,您必须
手动指定Electron标头的位置。

The native Node modules are supported by Electron, but since Electron is using a different V8 version from official Node, you have to manually specify the location of Electron's headers when building native modules.

我想这就是为什么您遇到 seg错误的原因。尝试按照文档中提到的电子接头构建模块:

I suppose that is why you are getting the seg fault. Try to build the module against the electron headers as mentioned in the docs:

npm install --save-dev electron-rebuild

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

这篇关于从电子应用程序打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆