Как напечатать файл docx на принтере, используя Node.js - PullRequest
0 голосов
/ 12 июня 2018

Я пытаюсь написать программу в node.js, которая позволит пользователю распечатать файл docx на принтере.

Кто-нибудь, кто может показать мне, как это делается в Node.js?

1 Ответ

0 голосов
/ 12 июня 2018

Вы можете использовать модуль node-printer .

Вот пример того, как использовать его для печати файла:

// use: node printFile.js [filePath printerName]
var printer = require("printer"),
    filename = process.argv[2] || __filename;

console.log('platform:', process.platform);
console.log('try to print file: ' + filename);

if( process.platform != 'win32') {
  printer.printFile({filename:filename,
    printer: process.env[3], // printer name, if missing then will print to default printer
    success:function(jobID){
      console.log("sent to printer with ID: "+jobID);
    },
    error:function(err){
      console.log(err);
    }
  });
} else {
  // not yet implemented, use printDirect and text
  var fs = require('fs');
  printer.printDirect({data:fs.readFileSync(filename),
    printer: process.env[3], // printer name, if missing then will print to default printer
    success:function(jobID){
      console.log("sent to printer with ID: "+jobID);
    },
    error:function(err){
      console.log(err);
    }
  });
}
...