Skip to content

Commit

Permalink
Refactored driver_options retrieval into a separate method `getPrinte…
Browse files Browse the repository at this point in the history
…rDriverOptions`

Made possible an unlimited supply of CUPS options via a JS object passed into printDirect
Modified README and examples accordingly
Fixed a little bug with getPrinter not returning a value (introduced by the correcting function for printing info)
  • Loading branch information
alexeyst committed Mar 10, 2015
1 parent a5f1f4c commit fb35d38
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 112 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ I was involved in a project where I need to print from Node.JS. This is the reas
* compatible with node-webkit v0.8.x and 0.9.2;
* ```getPrinters()``` to enumerate all installed printers with current jobs and statuses;
* ```getPrinter(printerName)``` to get a specific/default printer info with current jobs and statuses;
* ```getPrinterDriverOptions(printerName)``` to get a specific/default printer driver options such as supported paper size and other info
* ```getSelectedPaperSize(printerName)``` to get a specific/default printer default paper size from its driver options
* ```getDefaultPrinterName()``` return the default printer name;
* ```printDirect(stringData|bufferData, printerName, format, docname, success, error)``` to send a job to a specific/default printer;
* ```getSupportedPrintFormats()``` to get all posibilbe print formats for printDirect method which depends on OS. ```RAW``` and ```TEXT``` are supported from all OS-es;
* ```printDirect(stringData|bufferData, printerName, format, docname, options, success, error)``` to send a job to a specific/default printer, now supports [CUPS options](http://www.cups.org/documentation.php/options.html) passed in the form of a JS object (see `cancelJob.js` example);
* ```getSupportedPrintFormats()``` to get all possible print formats for printDirect method which depends on OS. ```RAW``` and ```TEXT``` are supported from all OS-es;
* ```getJob(printerName, jobId)``` to get a specific job info including job status;
* ```setJob(printerName, jobId, command)``` to send a command to a job (e.g. ```'CANCEL'``` to cancel the job);
* ```getSupportedJobCommands()``` to get supported job commands for setJob() depends on OS. ```'CANCEL'``` command is supported from all OS-es.
Expand Down
5 changes: 5 additions & 0 deletions examples/cancelJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ printer.printDirect({
data:"print from Node.JS buffer", // or simple String: "some text"
printer:printerName, // printer name
type: printerFormat, // type: RAW, TEXT, PDF, JPEG, .. depends on platform
options: // supported page sizes may be retrieved using getPrinterDriverOptions, supports CUPS printing options
{
media: 'Letter',
'fit-to-page': true
},
success:function(jobID){
console.log("sent to printer with ID: "+jobID);
var jobInfo = printer.getJob(printerName, jobID);
Expand Down
166 changes: 87 additions & 79 deletions lib/printer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var printer_helper = {}
fs = require("fs")
child_process = require("child_process")
os = require("os")
path = require("path"),
fs = require("fs")
child_process = require("child_process")
os = require("os")
path = require("path"),
native_lib_path = path.join(__dirname, '../build/Release/node_printer.node'),
printer_helper;

Expand All @@ -18,7 +18,7 @@ module.exports.getPrinters = getPrinters;

/** send data to printer
*/
module.exports.printDirect = printDirect
module.exports.printDirect = printDirect;

/** Get supported print format for printDirect
*/
Expand All @@ -34,6 +34,7 @@ module.exports.getSupportedJobCommands = printer_helper.getSupportedJobCommands;
*/
module.exports.getPrinter = getPrinter;
module.exports.getSelectedPaperSize = getSelectedPaperSize;
module.exports.getPrinterDriverOptions = getPrinterDriverOptions;

/// Return default printer name
module.exports.getDefaultPrinterName = printer_helper.getDefaultPrinterName;
Expand All @@ -53,17 +54,34 @@ function getPrinter(printerName)
if(!printerName) {
printerName = printer_helper.getDefaultPrinterName();
}
var printer = printer_helper.getPrinter(printerName);
correctPrinterinfo(printer);
return printer;
}

return correctPrinterinfo(printer_helper.getPrinter(printerName));
/** Get printer driver options includes advanced options like supported paper size
* @param printerName printer name to extract the info (default printer used if printer is not provided)
* @return printer driver info:
*/
function getPrinterDriverOptions(printerName)
{
if(!printerName) {
printerName = printer_helper.getDefaultPrinterName();
}

return printer_helper.getPrinterDriverOptions(printerName);
}

// Finds selected paper size pertaining to the specific printer out of all supported ones in driver_options
/** Finds selected paper size pertaining to the specific printer out of all supported ones in driver_options
* @param printerName printer name to extract the info (default printer used if printer is not provided)
* @return selected paper size
*/
function getSelectedPaperSize(printerName){
var printer = getPrinter(printerName);
var driver_options = getPrinterDriverOptions(printerName);
var selectedSize = "";
if (printer.driver_options && printer.driver_options.PageSize) {
Object.keys(printer.driver_options.PageSize).forEach(function(key){
if (printer.driver_options.PageSize[key] === "true")
if (driver_options && driver_options.PageSize) {
Object.keys(driver_options.PageSize).forEach(function(key){
if (driver_options.PageSize[key] === "true")
selectedSize = key;
});
}
Expand All @@ -72,12 +90,12 @@ function getSelectedPaperSize(printerName){

function getJob(printerName, jobId)
{
return printer_helper.getJob(printerName, jobId);
return printer_helper.getJob(printerName, jobId);
}

function setJob(printerName, jobId, command)
{
return printer_helper.setJob(printerName, jobId, command);
return printer_helper.setJob(printerName, jobId, command);
}

function getPrinters(){
Expand Down Expand Up @@ -117,37 +135,34 @@ function correctPrinterinfo(printer) {
}

/*
print raw data. This function is intend to be asynchronous
parameters:
parameters - Object, parameters objects with the following structure:
data - String, mandatory, data to printer
printer - String, optional, name of the printer, if missing, will try to print to default printer
docname - String, optional, name of document showed in printer status
type - String, optional, only for wind32, data type, one of the RAW, TEXT
media - String, optional, page size, e.g. A4, Letter or other
fit_to_page - Bool, optional, used with `media` and if true scales the document to fit page
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
or
data - String, mandatory, data to printer
printer - String, optional, name of the printer, if missing, will try to print to default printer
docname - String, optional, name of document showed in printer status
type - String, optional, data type, one of the RAW, TEXT
media - String, optional, page size, e.g. A4, Letter or other
fit_to_page - Bool, optional, used with `media` and if true scales the document to fit page
success - Function, optional, callback function with first argument job_id
error - Function, optional, callback function if exists any error
*/
print raw data. This function is intend to be asynchronous
parameters:
parameters - Object, parameters objects with the following structure:
data - String, mandatory, data to printer
printer - String, optional, name of the printer, if missing, will try to print to default printer
docname - String, optional, name of document showed in printer status
type - String, optional, only for wind32, data type, one of the RAW, TEXT
options - JS object with CUPS options, optional
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
or
data - String, mandatory, data to printer
printer - String, optional, name of the printer, if missing, will try to print to default printer
docname - String, optional, name of document showed in printer status
type - String, optional, data type, one of the RAW, TEXT
options - JS object with CUPS options, optional
success - Function, optional, callback function with first argument job_id
error - Function, optional, callback function if exists any error
*/
function printDirect(parameters){
var data = parameters
, printer
, docname
, type
, media
, fit_to_page
, options
, success
, error;

Expand All @@ -158,57 +173,50 @@ function printDirect(parameters){
printer = parameters.printer;
docname = parameters.docname;
type = parameters.type;
media = parameters.media;
fit_to_page = parameters.fit_to_page;
options = parameters.options;
success = parameters.success;
error = parameters.error;
}else{
printer = arguments[1];
type = arguments[2];
docname = arguments[3];
media = arguments[4];
fit_to_page = arguments[5];
success = arguments[6];
error = arguments[7];
options = arguments[4];
success = arguments[5];
error = arguments[6];
}

if(!type){
type = "RAW";
}
if(!type){
type = "RAW";
}

// Set default printer name
if(!printer) {
printer = printer_helper.getDefaultPrinterName();
}
// Set default printer name
if(!printer) {
printer = printer_helper.getDefaultPrinterName();
}

type = type.toUpperCase();

if(!docname){
docname = "node print job";
}

if (!media){
media = "";
}

if (fit_to_page && fit_to_page !== "false")
fit_to_page = "true";
else
fit_to_page = "false";

//TODO: check parameters type
if(printer_helper.printDirect){// call C++ binding
try{
var res = printer_helper.printDirect(data, printer, docname, type, media, fit_to_page, success, error);
if(res){
success(res);
}else{
error(Error("Something wrong in printDirect"));
}
}catch (e){
error(e);
}
if(!docname){
docname = "node print job";
}

if (!options){
options = {};
}

//TODO: check parameters type
if(printer_helper.printDirect){// call C++ binding
try{
var res = printer_helper.printDirect(data, printer, docname, type, options);
if(res){
success(res);
}else{
error(Error("Something wrong in printDirect"));
}
}catch (e){
error(e);
}
}else{
error("Not supported");
}
error("Not supported");
}
}
6 changes: 6 additions & 0 deletions src/macros.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
} \
v8::Local<v8::External> var = v8::Local<v8::External>::Cast(args[i]);

#define REQUIRE_ARGUMENT_OBJECT(args, i, var) \
if (args.Length() <= (i) || !args[i]->IsObject()) { \
RETURN_EXCEPTION_STR("Argument " #i " is not an object"); \
} \
v8::Local<v8::Object> var = v8::Local<v8::Object>::Cast(args[i]);


#define REQUIRE_ARGUMENT_FUNCTION(i, var) \
if (args.Length() <= (i) || !args[i]->IsFunction()) { \
Expand Down
1 change: 1 addition & 0 deletions src/node_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ void initNode(v8::Handle<v8::Object> exports) {
NODE_SET_METHOD(exports, "getPrinters", getPrinters);
NODE_SET_METHOD(exports, "getDefaultPrinterName", getDefaultPrinterName);
NODE_SET_METHOD(exports, "getPrinter", getPrinter);
NODE_SET_METHOD(exports, "getPrinterDriverOptions", getPrinterDriverOptions);
NODE_SET_METHOD(exports, "getJob", getJob);
NODE_SET_METHOD(exports, "setJob", setJob);
NODE_SET_METHOD(exports, "printDirect", PrintDirect);
Expand Down
5 changes: 5 additions & 0 deletions src/node_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ MY_NODE_MODULE_CALLBACK(getDefaultPrinterName);
*/
MY_NODE_MODULE_CALLBACK(getPrinter);

/** Retrieve printer driver info
* @param printer name String
*/
MY_NODE_MODULE_CALLBACK(getPrinterDriverOptions);

/** Retrieve job info
* @param printer name String
* @param job id Number
Expand Down
Loading

0 comments on commit fb35d38

Please sign in to comment.