forked from BuildingIntelligence/dymo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
145 lines (119 loc) · 4.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var path = require('path'),
fs = require('fs'),
edge = require('electron-edge-js'),
activePrinter,
initReady = false;
//
// Lets Make sure the Libraries are here
var libDir = path.join(__dirname, 'lib'),
nodeDymoLib = path.join(libDir, 'NodeDymoLib.dll'),
dymoLibPath = path.join('C:', 'Program Files (x86)', 'DYMO', 'DYMO Label Software', 'Framework'),
dymoAssemblies = [ 'DYMO.Label.Framework.dll', 'DYMO.DLS.Runtime.dll', 'DYMO.Common.dll', 'DYMOPrinting.dll', 'PrintingSupportLibrary.dll' ],
libsMoved = 0;
var initDymoLib = function() {
if(++libsMoved < dymoAssemblies.length){ return false; }
initReady = true;
return true;
}
for( var f of dymoAssemblies ){
if( fs.existsSync( path.join(libDir, f) ) ){ initDymoLib(); continue; } // Only fetch these files once
console.log( 'Dymo Assembly ' + f );
var source = path.join(dymoLibPath, f),
target = path.join(libDir, f);
var readStream = fs.createReadStream(source);
var writeStream = fs.createWriteStream(target);
readStream.on('error', function(err) { throw err });
writeStream.on('error', function(err) { throw err });
writeStream.on('finish', initDymoLib);
readStream.pipe(writeStream);
}
// Lets Make sure the Libraries are here
//
var ready = module.exports.ready = function(){
console.log("Printer-Dymo: ready() triggered");
return initReady;
}
/** Get a list of all the printers
* @return printer object info:
*/
var getPrintersAsync = module.exports.getPrintersAsync = function( callback ){
console.log("Printer-Dymo: getPrinterAsync() triggered");
if( initReady != true ){ }
var availablePrinters = edge.func({
assemblyFile: nodeDymoLib,
typeName: 'NodeDymoLib.Dymo',
methodName: 'GetPrintersAsync'
});
availablePrinters( '', callback );
}
/**
* Same as getPrinters but syncronice
*/
var getPrinters = module.exports.getPrinters = function(){
console.log("Printer-Dymo: getPrinters() triggered");
if( initReady != true ){ }
var availablePrinters = edge.func({
assemblyFile: nodeDymoLib,
typeName: 'NodeDymoLib.Dymo',
methodName: 'GetPrinters'
});
return availablePrinters( '', true );
}
var getPrinter = module.exports.getPrinterSync = function( thisPrinterName ){
if( thisPrinterName == '' ){ return true; }
var availablePrinters = edge.func({
assemblyFile: nodeDymoLib,
typeName: 'NodeDymoLib.Dymo',
methodName: 'GetPrinters'
});
var tempPrinters = availablePrinters( '', true );
for( var i in tempPrinters ){
console.log( tempPrinters[i] );
if( tempPrinters[i].Name == thisPrinterName ){
return tempPrinters[i];
}
}
return false;
}
var setPrinter = module.exports.setPrinter = function( printerName ){
// TODO: verify that this printer exists and return errors as needed.
activePrinter = setPrinter;
}
/** Print out the label provided
* parameters: Object, parameters objects with the following structure:
printer - String [Optional] - Name of the printer, if missing, will attempt to use the activePrinter (set by using setPrinter).
jobTitle - String [Optional] - Name of the job to show up in the print queue (default: "Dymo Labels")
quality - Integer [Optional] - Level of quality to print out: 0 = Text, 1 = BarcodeAndGraphics, & 2 = Auto. (default: Auto)
copies - Integer [Optional] - Number of copies to print (default: 1)
labels - Object [Required] - One entry per label to print in this job.
filename - String [Required]
fields - Object [Optional] - What fields should be updated to what values
images - Object [Optional] - Should be a buffer of PNG images with their related keys
callback: a standard callback of (err, result);
* @return printer object info:
*/
var print = module.exports.print = function( parameters, callback ){
if( typeof callback != 'function' ){ callback = function(err, data){} }
var dymoPrint = edge.func({
assemblyFile: nodeDymoLib,
typeName: 'NodeDymoLib.Dymo',
methodName: 'PrintLabelsAsync',
references: [
path.join(libDir, 'DYMO.Label.Framework.dll'),
path.join(libDir, 'DYMO.DLS.Runtime.dll'),
path.join(libDir, 'DYMO.Common.dll'),
path.join(libDir, 'DYMOPrinting.dll'),
path.join(libDir, 'PrintingSupportLibrary.dll'),
path.join(libDir, 'x64', 'DYMOPrinting.dll'),
path.join(libDir, 'x64', 'PrintingSupportLibrary.dll')
]
});
if( typeof parameters.printer == 'undefined' ){
if( activePrinter != null ){
parameters.printer = activePrinter
}else{
return callback( 'Must Define the `printer`' );
}
}
dymoPrint(parameters, callback);
}