thiagoelg / node-printer

Native node.js printer
125 stars 76 forks source link

How to cut after print #9

Closed jpnobrega closed 4 years ago

jpnobrega commented 4 years ago

How to cut after print?

var printer = require("./node_modules/@thiagoelg/node-printer/");
// use: node printFile.js [filePath printerName]

var 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);
    }
  });
}
thiagoelg commented 4 years ago

Hi @jpnobrega, I think it's only possible to trigger the printer cut function when using type: 'RAW' on printDirect. For that, you'll need to create a Buffer with the contents you want to print, and then, add the cutting command to that Buffer, and send it via the data property of the object passed to printDirect.

Here's a discussion on the original node-printer repository that may help you on how to do that: https://github.com/tojocky/node-printer/issues/54

This may also help: https://github.com/tojocky/node-printer/issues/25

Here's an example on printing with type: 'RAW' (but not cutting): https://github.com/thiagoelg/node-printer/blob/master/examples/print_raw.js