Commit 652e00f9 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

Merge branch 'SW-Shawn.Liu' into 'master'

Add printserverWin.js and printer module.

See merge request !1
parents bfe68a04 08e2c981
var printer = require("../lib"),
util = require('util'),
printers = printer.getPrinters();
printers.forEach(function(iPrinter, i){
console.log('' + i + 'ppd for printer "' + iPrinter.name + '":' + util.inspect(printer.getPrinterDriverOptions(iPrinter.name), {colors:true, depth:10} ));
console.log('\tselected page size:'+ printer.getSelectedPaperSize(iPrinter.name) + '\n');
});
var printer = require("../lib"),
util = require('util');
console.log("installed printers:\n"+util.inspect(printer.getPrinters(), {colors:true, depth:10}));
var printer = require("../lib"),
util = require('util');
console.log("supported formats are:\n"+util.inspect(printer.getSupportedPrintFormats(), {colors:true, depth:10}));
var printer = require("../lib"), //=require('pritner')
util = require('util');
console.log("supported job commands:\n"+util.inspect(printer.getSupportedJobCommands(), {colors:true, depth:10}));
// use: node printFile.js [filePath printerName]
var printer = require("../lib"),
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);
}
});
}
// Windows does not support PDF formats, but you can use imagemagick-native to achieve conversion from PDF to EMF.
var printer = require("../lib"),
fs = require('fs'),
path = require('path'),
filename = process.argv[2],
printername = process.argv[3];
if(process.platform == 'win32') {
throw 'Not yet supported for win32'
}
if(!filename || filename == '-h') {
throw 'PDF file name is missing. Please use the following params: <filename> [printername]'
}
filename = path.resolve(process.cwd(), filename);
console.log('printing file name ' + filename);
fs.readFile(filename, function(err, data){
if(err) {
console.error('err:' + err);
return;
}
console.log('data type is: '+typeof(data) + ', is buffer: ' + Buffer.isBuffer(data));
printer.printDirect({
data: data,
type: 'PDF',
success: function(id) {
console.log('printed with id ' + id);
},
error: function(err) {
console.error('error on printing: ' + err);
}
})
});
// Windows does not support PDF formats, but you can use imagemagick-native to achieve conversion from PDF to EMF.
var printer = require("../lib"),
imagemagick, // will be loaded later with proper error.
fs = require('fs'),
filename = process.argv[2],
printername = process.argv[2];
if(process.platform !== 'win32') {
throw 'This application can be run only on win32 as a demo of print PDF image'
}
if(!filename) {
throw 'PDF file name is missing. Please use the following params: <filename> [printername]'
}
try {
imagemagick = require('imagemagick-native');
} catch(e) {
throw 'please install imagemagick-native: `npm install imagemagick-native`'
}
var data = fs.readFileSync(filename);
console.log('data: ' + data.toString().substr(0, 20));
//console.log(imagemagick.identify({srcData: data}));
// First convert PDF into
imagemagick.convert({
srcData: data,
srcFormat: 'PDF',
format: 'EMF',
}, function(err, buffer) {
if (err) {
throw 'something went wrong on converting to EMF: ' + err;
}
// Now we have EMF file, send it to printer as EMF format
printer.printDirect({
data: buffer,
type: 'EMF',
success: function(id) {
console.log('printed with id ' + id);
},
error: function(err) {
console.error('error on printing: ' + err);
}
})
})
var printer = require("../lib");
printer.printDirect({data:"print from Node.JS buffer" // or simple String: "some text"
//, printer:'Foxit Reader PDF Printer' // printer name, if missing then will print to default printer
, 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);}
});
module.exports = require('./printer');
\ No newline at end of file
var printer_helper = {},
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;
if(fs.existsSync(native_lib_path)) {
printer_helper = require(native_lib_path);
} else {
printer_helper = require('./node_printer_'+process.platform+'_'+process.arch+'.node');
}
/** Return all installed printers including active jobs
*/
module.exports.getPrinters = getPrinters;
/** send data to printer
*/
module.exports.printDirect = printDirect;
/// send file to printer
module.exports.printFile = printFile;
/** Get supported print format for printDirect
*/
module.exports.getSupportedPrintFormats = printer_helper.getSupportedPrintFormats;
/**
* Get possible job command for setJob. It depends on os.
* @return Array of string. e.g.: DELETE, PAUSE, RESUME
*/
module.exports.getSupportedJobCommands = printer_helper.getSupportedJobCommands;
/** get printer info object. It includes all active jobs
*/
module.exports.getPrinter = getPrinter;
module.exports.getSelectedPaperSize = getSelectedPaperSize;
module.exports.getPrinterDriverOptions = getPrinterDriverOptions;
/// Return default printer name
module.exports.getDefaultPrinterName = getDefaultPrinterName;
/** get printer job info object
*/
module.exports.getJob = getJob;
module.exports.setJob = setJob;
/**
* return user defined printer, according to https://www.cups.org/documentation.php/doc-2.0/api-cups.html#cupsGetDefault2 :
* "Applications should use the cupsGetDests and cupsGetDest functions to get the user-defined default printer,
* as this function does not support the lpoptions-defined default printer"
*/
function getDefaultPrinterName() {
var printerName = printer_helper.getDefaultPrinterName();
if(printerName) {
return printerName;
}
// seems correct posix behaviour
var printers= getPrinters();
for(i in printers) {
var printer = printers[i];
if(printer.isDefault === true) {
return printer.name;
}
}
// printer not found, return nothing(undefined)
}
/** Get printer info with jobs
* @param printerName printer name to extract the info
* @return printer object info:
* TODO: to enum all possible attributes
*/
function getPrinter(printerName)
{
if(!printerName) {
printerName = getDefaultPrinterName();
}
var printer = printer_helper.getPrinter(printerName);
correctPrinterinfo(printer);
return printer;
}
/** 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 = getDefaultPrinterName();
}
return printer_helper.getPrinterDriverOptions(printerName);
}
/** 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 driver_options = getPrinterDriverOptions(printerName);
var selectedSize = "";
if (driver_options && driver_options.PageSize) {
Object.keys(driver_options.PageSize).forEach(function(key){
if (driver_options.PageSize[key])
selectedSize = key;
});
}
return selectedSize;
}
function getJob(printerName, jobId)
{
return printer_helper.getJob(printerName, jobId);
}
function setJob(printerName, jobId, command)
{
return printer_helper.setJob(printerName, jobId, command);
}
function getPrinters(){
var printers = printer_helper.getPrinters();
for(i in printers){
correctPrinterinfo(printers[i]);
}
return printers;
}
function correctPrinterinfo(printer) {
if(printer.status || !printer.options || !printer.options['printer-state']){
return;
}
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'
}
// correct date type
var k;
for(k in printer.options) {
if(/time$/.test(k) && printer.options[k] && !(printer.options[k] instanceof Date)) {
printer.options[k] = new Date(printer.options[k] * 1000);
}
}
printer.status = status;
}
/*
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
, options
, success
, error;
if(arguments.length==1){
//TODO: check parameters type
//if (typeof parameters )
data = parameters.data;
printer = parameters.printer;
docname = parameters.docname;
type = parameters.type;
options = parameters.options||{};
success = parameters.success;
error = parameters.error;
}else{
printer = arguments[1];
type = arguments[2];
docname = arguments[3];
options = arguments[4];
success = arguments[5];
error = arguments[6];
}
if(!type){
type = "RAW";
}
// Set default printer name
if(!printer) {
printer = getDefaultPrinterName();
}
type = type.toUpperCase();
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");
}
}
/**
parameters:
parameters - Object, parameters objects with the following structure:
filename - String, mandatory, data to printer
docname - String, optional, name of document showed in printer status
printer - String, optional, mane of the printer, if missed, will try to retrieve the default printer name
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
*/
function printFile(parameters){
var filename,
docname,
printer,
options,
success,
error;
if((arguments.length !== 1) || (typeof(parameters) !== 'object')){
throw new Error('must provide arguments object');
}
filename = parameters.filename;
docname = parameters.docname;
printer = parameters.printer;
options = parameters.options || {};
success = parameters.success;
error = parameters.error;
if(!success){
success = function(){};
}
if(!error){
error = function(err){
throw err;
};
}
if(!filename){
var err = new Error('must provide at least a filename');
return error(err);
}
// try to define default printer name
if(!printer) {
printer = getDefaultPrinterName();
}
if(!printer) {
return error(new Error('Printer parameter of default printer is not defined'));
}
// set filename if docname is missing
if(!docname){
docname = filename;
}
//TODO: check parameters type
if(printer_helper.printFile){// call C++ binding
try{
// TODO: proper success/error callbacks from the extension
var res = printer_helper.printFile(filename, docname, printer, options);
if(!isNaN(parseInt(res))) {
success(res);
} else {
error(Error(res));
}
} catch (e) {
error(e);
}
} else {
error("Not supported");
}
}
{
"_args": [
[
{
"raw": "printer",
"scope": null,
"escapedName": "printer",
"name": "printer",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"C:\\MIT\\fablab\\PrintServer"
]
],
"_from": "printer@latest",
"_id": "printer@0.2.2",
"_inCache": true,
"_location": "/printer",
"_nodeVersion": "5.2.0",
"_npmOperationalInternal": {
"host": "packages-12-west.internal.npmjs.com",
"tmp": "tmp/printer-0.2.2.tgz_1473484407703_0.04797186842188239"
},
"_npmUser": {
"name": "ionlupascu",
"email": "ionlupascu@gmail.com"
},
"_npmVersion": "3.10.5",
"_phantomChildren": {},
"_requested": {
"raw": "printer",
"scope": null,
"escapedName": "printer",
"name": "printer",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/printer/-/printer-0.2.2.tgz",
"_shasum": "830b8f5c97b97fd9b464d4f4e105d43337792296",
"_shrinkwrap": null,
"_spec": "printer",
"_where": "C:\\MIT\\fablab\\PrintServer",
"author": {
"name": "Ion Lupascu",
"email": "ionlupascu@gmail.com",
"url": "http://program-support.co.uk/"
},
"bugs": {
"url": "https://github.com/tojocky/node-printer/issues"
},
"contributors": [
{
"name": "Name Lastname",
"email": "email@server.com"
}
],
"dependencies": {},
"description": "Node.js printer bindings",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-jshint": "^0.11.0",
"grunt-node-gyp": "git://github.com/tojocky/grunt-node-gyp.git",
"grunt-nw-gyp": "git://github.com/tojocky/grunt-nw-gyp.git",
"nodeunit": "*"
},
"directories": {},
"dist": {
"shasum": "830b8f5c97b97fd9b464d4f4e105d43337792296",
"tarball": "https://registry.npmjs.org/printer/-/printer-0.2.2.tgz"
},
"engines": {
"node": ">= 0.8.0"
},
"gitHead": "c0796566ec78773697a4ce78f04965ccc7864b4e",
"gypfile": true,
"homepage": "http://github.com/tojocky/node-printer",
"licenses": [
{
"type": "BSD"
}
],
"main": "./lib/printer",
"maintainers": [
{
"name": "ionlupascu",
"email": "ionlupascu@gmail.com"
}
],
"name": "printer",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git://github.com/tojocky/node-printer.git"
}, </