Skip to content

Commit

Permalink
continue getPrinters on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tojocky committed Jun 10, 2014
1 parent 1617b7b commit cb1cb60
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 125 deletions.
24 changes: 15 additions & 9 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
'src/node_printer_win.cc'
],
'conditions': [
['OS != "win"', {
'sources!': [
# Linux-only; exclude on other platforms.
'src/node_printer_win.cc'
]}
],
['OS != "posix"', {
['OS == "win32"', {
'sources!': [
# posixx-only; exclude on other platforms.
'src/node_printer_posix.cc'
]}
]
]
}, { # != win32
'sources!': [
# Linux-only; exclude on other platforms.
'src/node_printer_win.cc'
],
'cflags':[
#run: 'cups-config --cflags'
],
'ldflags':[
#run: 'cups-config --libs'
'-lcups -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lz -lpthread -lm -lcrypt -lz'
]
}]
]
}
]
Expand Down
9 changes: 0 additions & 9 deletions examples/example_xps_windows.js

This file was deleted.

3 changes: 3 additions & 0 deletions examples/getPrinters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var printer = require("../lib"),
util = require('util');
console.log("installed printers:\n"+util.inspect(printer.getPrinters(), {colors:true, depth:10}));
10 changes: 10 additions & 0 deletions examples/print_raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var printer = require("../lib");

printer.printDirect({data:new Buffer("print from Node.JS buffer") // or simple String: "some text"
, printer:'raw_printer' // printer name
, type: 'RAW' // type: RAW, TEXT, PDF, JPEG, .. depends on platform
, success:function(jobID){
console.log("sent to printer with ID: "+jobID);
}
, error:function(err){console.log(err);}
});
57 changes: 30 additions & 27 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ var printer_helper = {}
, os = require("os")
, path = require("path");

if(process.platform=="win32"){
printer_helper = require('../build/Release/node_printer.node');
}
printer_helper = require('../build/Release/node_printer.node');

module.exports.getPrinters = printer_helper.getPrinters;
module.exports.getPrinters = getPrinters;
module.exports.printDirect = printDirect
module.exports.getSupportedFormats = printer_helper.getSupportedFormats;

function getPrinters(){
var printers = printer_helper.getPrinters();
for(i in printers){
var printer = printers[i];
if(printer.status || !printer.options || !printer.options['printer-state']){
continue;
}
var status = printer.options['printer-state'];
// Add posix status
if(status == '3'){
status = 'IDLE'
}
else if(status == '4'){
status = 'PRINTING'
}
else if(status == '5'){
status = 'STOPPED'
}
printer.status = status;
}
return printers;
}

/*
print raw data. This function is intend to be asynchronous
Expand All @@ -30,7 +51,7 @@ parameters:
printer - String, mandatory, mane of the printer
docname - String, optional, name of document showed in printer status
type - String, optional, data type, one of the RAW, TEXT
success - Function, optional, callback function
success - Function, optional, callback function with first argument job_id
error - Function, optional, callback function if exists any error
*/
function printDirect(parameters){
Expand Down Expand Up @@ -79,35 +100,17 @@ function printDirect(parameters){
}

//TODO: check parameters type
if((process.platform=="win32")||(printer_helper.printDirect)){// call C++ binding
if(!printer_helper.printDirect){
error("Not supported, try to compile this package with MSC");
return;
}
if(printer_helper.printDirect){// call C++ binding
try{
var res = printer_helper.printDirect(data, printer, docname, type, success, error);
if(res===true){
success();
if(res){
success(res);
}else{
error(Error("Something wrong"));
error(Error("Something wrong in printDirect"));
}
}catch (e){
error(e);
}
}else if (!printer_helper.printDirect){// should be POSIX
var temp_file_name = path.join(os.tmpDir(),"printing");
fs.writeFileSync(temp_file_name, data);
child_process.execFile('lpr', ['-P' + printer, '-oraw', '-r', temp_file_name], function(err, stdout, stderr){
if (err !== null) {
error('ERROR: ' + err);
return;
}
if (stderr) {
error('STD ERROR: ' + stderr);
return;
}
success();
});
}else{
error("Not supported");
}
Expand Down
39 changes: 9 additions & 30 deletions src/node_printer_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@ v8::Handle<v8::Value> getPrinters(const v8::Arguments& iArgs)
for(j = 0, dest_option = printer->options; j < printer->num_options; ++j, ++dest_option)
{
result_printer_options->Set(v8::String::NewSymbol(dest_option->name), v8::String::New(dest_option->value));

if(!strcmp(dest_option->name, "printer-state"))
{
if(!strcmp(dest_option->value, "3"))
{
result_printer->Set(v8::String::NewSymbol("status"), v8::String::New("IDLE"));
}
else if(!strcmp(dest_option->value, "4"))
{
result_printer->Set(v8::String::NewSymbol("status"), v8::String::New("PRINTING"));
}
else if(!strcmp(dest_option->value, "5"))
{
result_printer->Set(v8::String::NewSymbol("status"), v8::String::New("STOPPED"));
}
}
if(!strcmp(dest_option->name, "printer-state-reasons"))
{
result_printer->Set(v8::String::NewSymbol("statusReason"), v8::String::New(dest_option->value));
}
}
result_printer->Set(v8::String::NewSymbol("options"), result_printer_options);
result->Set(i, result_printer);
Expand Down Expand Up @@ -79,24 +59,22 @@ v8::Handle<v8::Value> PrintDirect(const v8::Arguments& iArgs) {
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Argument 0 missing")));
}

char* data(NULL);
size_t data_len(0);
std::string data;
v8::Handle<v8::Value> arg0(iArgs[0]);

if(arg0->IsString())
{
v8::String::Utf8Value data_str(arg0->ToString());
data = *data_str;
data_len = data_str.length();
v8::String::Utf8Value data_str_v8(arg0->ToString());
data.assign(*data_str_v8, data_str_v8.length());
}
else if(arg0->IsObject() && arg0.As<v8::Object>()->HasIndexedPropertiesInExternalArrayData())
{
data = static_cast<char*>(arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayData());
data_len = arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayDataLength();
data.assign(static_cast<char*>(arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayData()),
arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayDataLength());
}
else
{
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Argument 0 must be a string or NativeBuffer")));
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Argument 0 must be a string or Buffer")));
}

REQUIRE_ARGUMENT_STRING(iArgs, 1, printername);
Expand Down Expand Up @@ -140,9 +118,10 @@ v8::Handle<v8::Value> PrintDirect(const v8::Arguments& iArgs) {
int job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, *printername, *docname, num_options, options);
if(job_id > 0)
{
cupsStartDocument(CUPS_HTTP_DEFAULT, *printername, job_id, *docname, type_str.c_str(), true /*last document*/);
cupsStartDocument(CUPS_HTTP_DEFAULT, *printername, job_id, *docname, type_str.c_str(), 1 /*last document*/);
/* cupsWriteRequestData can be called as many times as needed */
cupsWriteRequestData(CUPS_HTTP_DEFAULT, data, data_len);
//TODO: to split big buffer
cupsWriteRequestData(CUPS_HTTP_DEFAULT, data.c_str(), data.size());
cupsFinishDocument(CUPS_HTTP_DEFAULT, *printername);
}
return scope.Close(v8::Number::New(job_id));
Expand Down
Loading

0 comments on commit cb1cb60

Please sign in to comment.