【问题标题】:How to print a docx file to a printer using Node.js如何使用 Node.js 将 docx 文件打印到打印机
【发布时间】:2018-06-12 12:23:39
【问题描述】:

我正在尝试在 node.js 中编写一个允许用户将 docx 文件打印到打印机的程序。

谁能告诉我它是如何在 Node.js 中完成的?

【问题讨论】:

    标签: javascript node.js printing printers


    【解决方案1】:

    您可以使用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);
        }
      });
    }
    

    【讨论】:

    • printer 不是一个模块,当我把它改成require('node-printer') printDirect 不是一个函数............
    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    相关资源
    最近更新 更多