@esslassi/electron-printer 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/binding.js +244 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
package/lib/binding.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
let addon = {}, binary_path;
|
|
4
|
+
switch (os.platform()) {
|
|
5
|
+
case 'win32':
|
|
6
|
+
if (os.arch() === 'ia32') {
|
|
7
|
+
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
8
|
+
} else if (os.arch() === 'x64') {
|
|
9
|
+
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
10
|
+
}
|
|
11
|
+
addon = require(binary_path);
|
|
12
|
+
break;
|
|
13
|
+
case 'darwin':
|
|
14
|
+
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
15
|
+
addon = require(binary_path);
|
|
16
|
+
break;
|
|
17
|
+
case 'linux':
|
|
18
|
+
if (os.arch() === 'ia32') {
|
|
19
|
+
addon = path.join(__dirname, 'electron-printer.node');
|
|
20
|
+
} else if (os.arch() === 'x64') {
|
|
21
|
+
addon = path.join(__dirname, 'electron-printer.node');
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
default:
|
|
25
|
+
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
26
|
+
addon = require(binary_path);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports.sayMyName = addon.SayMyName
|
|
30
|
+
module.exports.getPrinters = addon.getPrinters
|
|
31
|
+
module.exports.printDirect = printDirect
|
|
32
|
+
module.exports.getDefaultPrinterName = addon.getDefaultPrinterName
|
|
33
|
+
module.exports.getPrinter = getPrinter;
|
|
34
|
+
/// send file to printer
|
|
35
|
+
module.exports.printFile = printFile;
|
|
36
|
+
/** Get supported print format for printDirect
|
|
37
|
+
*/
|
|
38
|
+
module.exports.getSupportedPrintFormats = addon.getSupportedPrintFormats;
|
|
39
|
+
/*
|
|
40
|
+
print raw data. This function is intend to be asynchronous
|
|
41
|
+
|
|
42
|
+
parameters:
|
|
43
|
+
parameters - Object, parameters objects with the following structure:
|
|
44
|
+
data - String, mandatory, data to printer
|
|
45
|
+
printer - String, optional, name of the printer, if missing, will try to print to default printer
|
|
46
|
+
docname - String, optional, name of document showed in printer status
|
|
47
|
+
type - String, optional, only for wind32, data type, one of the RAW, TEXT
|
|
48
|
+
options - JS object with CUPS options, optional
|
|
49
|
+
success - Function, optional, callback function
|
|
50
|
+
error - Function, optional, callback function if exists any error
|
|
51
|
+
|
|
52
|
+
or
|
|
53
|
+
|
|
54
|
+
data - String, mandatory, data to printer
|
|
55
|
+
printer - String, optional, name of the printer, if missing, will try to print to default printer
|
|
56
|
+
docname - String, optional, name of document showed in printer status
|
|
57
|
+
type - String, optional, data type, one of the RAW, TEXT
|
|
58
|
+
options - JS object with CUPS options, optional
|
|
59
|
+
success - Function, optional, callback function with first argument job_id
|
|
60
|
+
error - Function, optional, callback function if exists any error
|
|
61
|
+
*/
|
|
62
|
+
function printDirect(parameters){
|
|
63
|
+
var data = parameters
|
|
64
|
+
, printer
|
|
65
|
+
, docname
|
|
66
|
+
, type
|
|
67
|
+
, options
|
|
68
|
+
, success
|
|
69
|
+
, error;
|
|
70
|
+
|
|
71
|
+
if(arguments.length==1){
|
|
72
|
+
//TODO: check parameters type
|
|
73
|
+
//if (typeof parameters )
|
|
74
|
+
data = parameters.data;
|
|
75
|
+
printer = parameters.printer;
|
|
76
|
+
docname = parameters.docname;
|
|
77
|
+
type = parameters.type;
|
|
78
|
+
options = parameters.options||{};
|
|
79
|
+
success = parameters.success;
|
|
80
|
+
error = parameters.error;
|
|
81
|
+
}else{
|
|
82
|
+
printer = arguments[1];
|
|
83
|
+
type = arguments[2];
|
|
84
|
+
docname = arguments[3];
|
|
85
|
+
options = arguments[4];
|
|
86
|
+
success = arguments[5];
|
|
87
|
+
error = arguments[6];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if(!type){
|
|
91
|
+
type = "RAW";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Set default printer name
|
|
95
|
+
if(!printer) {
|
|
96
|
+
printer = addon.getDefaultPrinterName();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type = type.toUpperCase();
|
|
100
|
+
|
|
101
|
+
if(!docname){
|
|
102
|
+
docname = "node print job";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!options){
|
|
106
|
+
options = {};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//TODO: check parameters type
|
|
110
|
+
if(addon.printDirect){// call C++ binding
|
|
111
|
+
try{
|
|
112
|
+
var res = addon.printDirect(data, printer, docname, type, options);
|
|
113
|
+
if(res){
|
|
114
|
+
success(res);
|
|
115
|
+
}else{
|
|
116
|
+
error(Error("Something wrong in printDirect"));
|
|
117
|
+
}
|
|
118
|
+
}catch (e){
|
|
119
|
+
error(e);
|
|
120
|
+
}
|
|
121
|
+
}else{
|
|
122
|
+
error("Not supported");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Get printer info with jobs
|
|
127
|
+
* @param printerName printer name to extract the info
|
|
128
|
+
* @return printer object info:
|
|
129
|
+
* TODO: to enum all possible attributes
|
|
130
|
+
*/
|
|
131
|
+
function getPrinter(printerName)
|
|
132
|
+
{
|
|
133
|
+
if(!printerName) {
|
|
134
|
+
printerName = addon.getDefaultPrinterName();
|
|
135
|
+
}
|
|
136
|
+
var printer = addon.getPrinter(printerName);
|
|
137
|
+
correctPrinterinfo(printer);
|
|
138
|
+
return printer;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
function correctPrinterinfo(printer) {
|
|
143
|
+
if(printer.status || !printer.options || !printer.options['printer-state']){
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
var status = printer.options['printer-state'];
|
|
148
|
+
// Add posix status
|
|
149
|
+
if(status == '3'){
|
|
150
|
+
status = 'IDLE'
|
|
151
|
+
}
|
|
152
|
+
else if(status == '4'){
|
|
153
|
+
status = 'PRINTING'
|
|
154
|
+
}
|
|
155
|
+
else if(status == '5'){
|
|
156
|
+
status = 'STOPPED'
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// correct date type
|
|
160
|
+
var k;
|
|
161
|
+
for(k in printer.options) {
|
|
162
|
+
if(/time$/.test(k) && printer.options[k] && !(printer.options[k] instanceof Date)) {
|
|
163
|
+
printer.options[k] = new Date(printer.options[k] * 1000);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
printer.status = status;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
parameters:
|
|
172
|
+
parameters - Object, parameters objects with the following structure:
|
|
173
|
+
filename - String, mandatory, data to printer
|
|
174
|
+
docname - String, optional, name of document showed in printer status
|
|
175
|
+
printer - String, optional, mane of the printer, if missed, will try to retrieve the default printer name
|
|
176
|
+
success - Function, optional, callback function
|
|
177
|
+
error - Function, optional, callback function if exists any error
|
|
178
|
+
*/
|
|
179
|
+
function printFile(parameters){
|
|
180
|
+
var filename,
|
|
181
|
+
docname,
|
|
182
|
+
printer,
|
|
183
|
+
options,
|
|
184
|
+
success,
|
|
185
|
+
error;
|
|
186
|
+
|
|
187
|
+
if((arguments.length !== 1) || (typeof(parameters) !== 'object')){
|
|
188
|
+
throw new Error('must provide arguments object');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
filename = parameters.filename;
|
|
192
|
+
docname = parameters.docname;
|
|
193
|
+
printer = parameters.printer;
|
|
194
|
+
options = parameters.options || {};
|
|
195
|
+
success = parameters.success;
|
|
196
|
+
error = parameters.error;
|
|
197
|
+
|
|
198
|
+
if(!success){
|
|
199
|
+
success = function(){};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if(!error){
|
|
203
|
+
error = function(err){
|
|
204
|
+
throw err;
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if(!filename){
|
|
209
|
+
var err = new Error('must provide at least a filename');
|
|
210
|
+
return error(err);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// try to define default printer name
|
|
214
|
+
if(!printer) {
|
|
215
|
+
printer = addon.getDefaultPrinterName();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if(!printer) {
|
|
219
|
+
return error(new Error('Printer parameter of default printer is not defined'));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// set filename if docname is missing
|
|
223
|
+
if(!docname){
|
|
224
|
+
docname = filename;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
//TODO: check parameters type
|
|
228
|
+
if(addon.printFile){// call C++ binding
|
|
229
|
+
try{
|
|
230
|
+
// TODO: proper success/error callbacks from the extension
|
|
231
|
+
var res = addon.printFile(filename, docname, printer, options);
|
|
232
|
+
|
|
233
|
+
if(!isNaN(parseInt(res))) {
|
|
234
|
+
success(res);
|
|
235
|
+
} else {
|
|
236
|
+
error(Error(res));
|
|
237
|
+
}
|
|
238
|
+
} catch (e) {
|
|
239
|
+
error(e);
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
error("Not supported");
|
|
243
|
+
}
|
|
244
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./binding');
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@esslassi/electron-printer",
|
|
3
3
|
"description": "Node API (N-API) supported electron.s || node.js printer bindings",
|
|
4
4
|
"homepage": "https://github.com/esslassi/electron-printer",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.3",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "types/index.d.ts",
|
|
8
8
|
"dependencies": {
|