Skip to content

Instantly share code, notes, and snippets.

@esteves67
Forked from vodolaz095/print.js
Created March 15, 2021 01:57
Show Gist options
  • Select an option

  • Save esteves67/9df2c89ccffc3f20da5d6b7933c253df to your computer and use it in GitHub Desktop.

Select an option

Save esteves67/9df2c89ccffc3f20da5d6b7933c253df to your computer and use it in GitHub Desktop.

Revisions

  1. @vodolaz095 vodolaz095 revised this gist Apr 11, 2013. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions print.js
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,10 @@ function getPrinterUrls(callback) {
    };

    function doPrintOnSelectedPrinter(printer, bufferToBePrinted, callback) {
    printer.execute("Get-Printer-Attributes", null, function(err, printerStatus){
    if(printerStatus['printer-attributes-tag']['printer-state']=='idle'){
    //printer ready to work
    //*/
    printer.execute("Print-Job",
    {
    "operation-attributes-tag":{
    @@ -48,10 +52,8 @@ function doPrintOnSelectedPrinter(printer, bufferToBePrinted, callback) {
    // console.log('Testins if job is ready. Try N '+tries);
    callback(null, job);//job is succesefully printed!
    }
    if (tries > 15) {//todo - change it to what you need!
    if (tries > 50) {//todo - change it to what you need!
    clearInterval(t);

    //vvv trying to cancel job
    printer.execute("Cancel-Job", {
    "operation-attributes-tag":{
    //"job-uri":jobUri, //uncomment this
    @@ -63,19 +65,22 @@ function doPrintOnSelectedPrinter(printer, bufferToBePrinted, callback) {
    }, function (err, res) {
    if (err) throw err;
    console.log('Job with id '+job["job-attributes-tag"]["job-id"]+'is being canceled');
    callback(new Error('Job is canceled - too many tries and job is not printed!'), null);
    });

    //^^^ trying to cancel job
    callback(new Error('Job is canceled - too many tries and job is not printed!'), null);

    }
    });

    }, 2000);
    } else {
    callback(new Error('Error sending job to printer!'), null);
    }
    });
    //*/
    } else {
    callback(new Error('Printer '+printerStatus['printer-attributes-tag']['printer-name']+' is not ready!'),null);
    }
    });
    }

    function doPrintOnAllPrinters(data, callback) {
  2. @vodolaz095 vodolaz095 revised this gist Apr 11, 2013. 1 changed file with 26 additions and 4 deletions.
    30 changes: 26 additions & 4 deletions print.js
    Original file line number Diff line number Diff line change
    @@ -40,15 +40,33 @@ function doPrintOnSelectedPrinter(printer, bufferToBePrinted, callback) {
    printer.execute("Get-Job-Attributes",
    {"operation-attributes-tag":{'job-uri':jobUri}},
    function (err2, job) {
    // console.log(job);
    if (err2) throw err2;
    tries++;
    if (job && job["job-attributes-tag"]["job-state"] == 'completed') {
    clearInterval(t);
    // console.log('Testins if job is ready. Try N '+tries);
    callback(null, job);//job is succesefully printed!
    }
    if (tries > 100) {//todo - change it to what you need!
    if (tries > 15) {//todo - change it to what you need!
    clearInterval(t);
    callback(new Error('Job is canceled - too many tries and job is not printed!'), null);

    //vvv trying to cancel job
    printer.execute("Cancel-Job", {
    "operation-attributes-tag":{
    //"job-uri":jobUri, //uncomment this
    //*/
    "printer-uri":printer.uri, //or uncomment this two lines - one of variants should work!!!
    "job-id":job["job-attributes-tag"]["job-id"]
    //*/
    }
    }, function (err, res) {
    if (err) throw err;
    console.log('Job with id '+job["job-attributes-tag"]["job-id"]+'is being canceled');
    callback(new Error('Job is canceled - too many tries and job is not printed!'), null);
    });

    //^^^ trying to cancel job
    }
    });

    @@ -57,6 +75,7 @@ function doPrintOnSelectedPrinter(printer, bufferToBePrinted, callback) {
    callback(new Error('Error sending job to printer!'), null);
    }
    });
    //*/
    }

    function doPrintOnAllPrinters(data, callback) {
    @@ -75,8 +94,11 @@ function doPrintOnAllPrinters(data, callback) {
    }

    /*
    Example of usage
    */
    Example of usage
    */




    fs.readFile('package.json', function (err, data) {
    doPrintOnAllPrinters(data, function (err, job) {
  3. @vodolaz095 vodolaz095 revised this gist Apr 10, 2013. 1 changed file with 41 additions and 12 deletions.
    53 changes: 41 additions & 12 deletions print.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,28 @@
    var ipp = require('ipp'); //get it from there - https://npmjs.org/package/ipp - $npm install ipp
    var request = require('request'); //get it from there - https://npmjs.org/package/request - $npm install request
    var fs = require('fs');
    // Get printers from here - http://localhost:631/printers
    // we need real printer url to use there
    //for example, i have printer Xerox Phaser 3010 on my local pc. it is accessible from this url in CUPS
    var printer = ipp.Printer("http://localhost:631/printers/Xerox-Phaser-3010");

    function getPrinterUrls(callback) {
    var CUPSurl = 'http://localhost:631/printers';//todo - change of you have CUPS running on other host
    request(CUPSurl, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    var printersMatches = body.match(/<TR><TD><A HREF="\/printers\/([a-zA-Z0-9-^"]+)">/gm);//i know, this is terrible, sorry(
    var printersUrls = [];
    var i;
    if (printersMatches) {
    for (i = 0; i < printersMatches.length; i++) {
    var a = (/"\/printers\/([a-zA-Z0-9-^"]+)"/).exec(printersMatches[i]);
    if (a) {
    printersUrls.push(CUPSurl + '/' + a[1]);
    }
    }
    }
    }
    callback(error, printersUrls);
    });
    };

    function doNapPrint(bufferToBePrinted, callback) {
    function doPrintOnSelectedPrinter(printer, bufferToBePrinted, callback) {
    printer.execute("Print-Job",
    {
    "operation-attributes-tag":{
    @@ -26,9 +42,9 @@ function doNapPrint(bufferToBePrinted, callback) {
    function (err2, job) {
    if (err2) throw err2;
    tries++;
    if (job["job-attributes-tag"]["job-state"] == 'completed') {
    if (job && job["job-attributes-tag"]["job-state"] == 'completed') {
    clearInterval(t);
    callback(null, job);
    callback(null, job);//job is succesefully printed!
    }
    if (tries > 100) {//todo - change it to what you need!
    clearInterval(t);
    @@ -43,14 +59,27 @@ function doNapPrint(bufferToBePrinted, callback) {
    });
    }

    function doPrintOnAllPrinters(data, callback) {
    var b = new Buffer(data, 'binary');
    getPrinterUrls(function (err, printers) {
    if (err) throw err;
    if (printers) {
    for (var i = 0; i < printers.length; i++) {
    var printer = ipp.Printer(printers[i]);
    doPrintOnSelectedPrinter(printer, b, callback);
    }
    } else {
    throw new Error('Unable to find printer. Do you have printer installed and accessible via CUPS?');
    }
    });
    }

    /*
    Example of usage
    */
    Example of usage
    */

    fs.readFile('package.json', function (err, data) {
    var b = new Buffer(data, 'binary');
    doNapPrint(b,
    function (err, job) {
    doPrintOnAllPrinters(data, function (err, job) {
    if (err) {
    console.error('Error printing');
    console.error(err);
  4. @vodolaz095 vodolaz095 created this gist Apr 6, 2013.
    63 changes: 63 additions & 0 deletions print.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    var ipp = require('ipp'); //get it from there - https://npmjs.org/package/ipp - $npm install ipp
    var fs = require('fs');
    // Get printers from here - http://localhost:631/printers
    // we need real printer url to use there
    //for example, i have printer Xerox Phaser 3010 on my local pc. it is accessible from this url in CUPS
    var printer = ipp.Printer("http://localhost:631/printers/Xerox-Phaser-3010");


    function doNapPrint(bufferToBePrinted, callback) {
    printer.execute("Print-Job",
    {
    "operation-attributes-tag":{
    "requesting-user-name":"nap",
    "job-name":"testing"
    },
    "job-attributes-tag":{},
    data:bufferToBePrinted
    },
    function (err, res) {
    if (res.statusCode == 'successful-ok') {
    var jobUri = res['job-attributes-tag']['job-uri'];
    var tries = 0;
    var t = setInterval(function () {
    printer.execute("Get-Job-Attributes",
    {"operation-attributes-tag":{'job-uri':jobUri}},
    function (err2, job) {
    if (err2) throw err2;
    tries++;
    if (job["job-attributes-tag"]["job-state"] == 'completed') {
    clearInterval(t);
    callback(null, job);
    }
    if (tries > 100) {//todo - change it to what you need!
    clearInterval(t);
    callback(new Error('Job is canceled - too many tries and job is not printed!'), null);
    }
    });

    }, 2000);
    } else {
    callback(new Error('Error sending job to printer!'), null);
    }
    });
    }

    /*
    Example of usage
    */

    fs.readFile('package.json', function (err, data) {
    var b = new Buffer(data, 'binary');
    doNapPrint(b,
    function (err, job) {
    if (err) {
    console.error('Error printing');
    console.error(err);
    } else {
    console.log('Printed. Job parameters are: ');
    console.log(job);
    }
    }
    );
    });