从电子申请打印

时间:2015-08-28 15:09:52

标签: javascript node.js printing electron

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

控制台输出: [1] 9860 segmentation fault (core dumped) node_modules/electron-prebuilt/dist/electron.

这是我正在运行的应用。我只将打印行添加到电子文档中提供的简单应用示例中:

var app = require('app');  // Module to control application life.
var BrowserWindow = require('browser-window');  // Module to create native browser window.
var printer = require('printer');

// Report crashes to our server.
require('crash-reporter').start();

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600});

  // and load the index.html of the app.
  mainWindow.loadUrl('file://' + __dirname + '/app/index.html');

  // Open the devtools.
  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);}
  });


  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
});

我错过了什么吗? 我自己尝试了节点打印机,并成功打印了一些乱码文本。

3 个答案:

答案 0 :(得分:8)

function reverseArray(array) { var newArray = []; for (i = array.length-1; i >= 0; i--) { newArray.push(array.indexOf(i)); }; return newArray; }; var testArray = ["Zach", 2, 3, 4, 5]; var result = reverseArray(testArray); console.log(result); 使用原生绑定并根据docs

  

Electron支持本机Node模块,但自Electron以来   正在使用官方Node的不同V8版本,你必须这样做   在构建时手动指定Electron标头的位置   本机模块。

我想这就是你获得node-printer的原因。尝试针对电子标题构建模块,如文档中所述:

seg fault

答案 1 :(得分:5)

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

答案 2 :(得分:3)

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

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

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

如果这个api不够用,你还想利用node-printer模块让我知道,我会用更长的答案来编辑这个回复,关于如何分叉和修复node-printer以便它是电子兼容的。