@esslassi/electron-printer 0.0.2 → 0.0.5
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/README.md +222 -53
- package/binding.gyp +51 -33
- package/package.json +47 -33
- package/src/linux_printer.cpp +171 -0
- package/src/linux_printer.h +21 -0
- package/src/mac_printer.cpp +166 -0
- package/src/mac_printer.h +22 -0
- package/src/main.cpp +22 -0
- package/src/print.cpp +272 -0
- package/src/printer_factory.cpp +20 -0
- package/src/printer_factory.h +13 -0
- package/src/printer_interface.h +29 -0
- package/src/windows_printer.cpp +210 -0
- package/src/windows_printer.h +35 -0
- package/.gitattributes +0 -2
- package/Gruntfile.js +0 -80
- package/LICENSE +0 -21
- package/binding.js +0 -244
- package/index.js +0 -1
- package/printer.js +0 -1
- package/src/hello_world.cc +0 -8
- package/src/macros.hh +0 -53
- package/src/node_printer.cc +0 -31
- package/src/node_printer.hpp +0 -77
- package/src/node_printer_win.cc +0 -586
- package/test/getDefaultPrinterName.test.js +0 -18
- package/test/getPrinters.test.js +0 -26
- package/test/incompleteFunctions.js +0 -56
- package/test/printDirect.test.js +0 -34
- package/test/sayMyName.test.js +0 -21
- package/tools/buildElectronLinux.sh +0 -10
- package/tools/buildElectronWindows.ps1 +0 -20
- package/tools/buildWindows.ps1 +0 -23
- package/tools/generateReleaseBuildsLinux.sh +0 -59
- package/tools/generateReleaseBuildsWindows.ps1 +0 -63
- package/tools/getSourceFiles.py +0 -12
- package/tools/remove_directory.py +0 -27
- package/tsconfig.json +0 -22
- package/types/index.d.ts +0 -56
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
#include "windows_printer.h"
|
|
2
|
+
#include <vector>
|
|
3
|
+
|
|
4
|
+
std::string WindowsPrinter::GetPrinterStatus(DWORD status)
|
|
5
|
+
{
|
|
6
|
+
if (status & PRINTER_STATUS_OFFLINE)
|
|
7
|
+
return "offline";
|
|
8
|
+
if (status & PRINTER_STATUS_ERROR)
|
|
9
|
+
return "error";
|
|
10
|
+
if (status & PRINTER_STATUS_PAPER_JAM)
|
|
11
|
+
return "paper-jam";
|
|
12
|
+
if (status & PRINTER_STATUS_PAPER_OUT)
|
|
13
|
+
return "paper-out";
|
|
14
|
+
if (status & PRINTER_STATUS_MANUAL_FEED)
|
|
15
|
+
return "manual-feed";
|
|
16
|
+
if (status & PRINTER_STATUS_PAPER_PROBLEM)
|
|
17
|
+
return "paper-problem";
|
|
18
|
+
if (status & PRINTER_STATUS_BUSY)
|
|
19
|
+
return "busy";
|
|
20
|
+
if (status & PRINTER_STATUS_PRINTING)
|
|
21
|
+
return "printing";
|
|
22
|
+
if (status & PRINTER_STATUS_OUTPUT_BIN_FULL)
|
|
23
|
+
return "output-bin-full";
|
|
24
|
+
if (status & PRINTER_STATUS_NOT_AVAILABLE)
|
|
25
|
+
return "not-available";
|
|
26
|
+
if (status & PRINTER_STATUS_WAITING)
|
|
27
|
+
return "waiting";
|
|
28
|
+
if (status & PRINTER_STATUS_PROCESSING)
|
|
29
|
+
return "processing";
|
|
30
|
+
if (status & PRINTER_STATUS_INITIALIZING)
|
|
31
|
+
return "initializing";
|
|
32
|
+
if (status & PRINTER_STATUS_WARMING_UP)
|
|
33
|
+
return "warming-up";
|
|
34
|
+
if (status & PRINTER_STATUS_TONER_LOW)
|
|
35
|
+
return "toner-low";
|
|
36
|
+
if (status & PRINTER_STATUS_NO_TONER)
|
|
37
|
+
return "no-toner";
|
|
38
|
+
if (status & PRINTER_STATUS_PAGE_PUNT)
|
|
39
|
+
return "page-punt";
|
|
40
|
+
if (status & PRINTER_STATUS_USER_INTERVENTION)
|
|
41
|
+
return "user-intervention";
|
|
42
|
+
if (status & PRINTER_STATUS_OUT_OF_MEMORY)
|
|
43
|
+
return "out-of-memory";
|
|
44
|
+
if (status & PRINTER_STATUS_DOOR_OPEN)
|
|
45
|
+
return "door-open";
|
|
46
|
+
return "ready";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
std::wstring WindowsPrinter::Utf8ToWide(const std::string &str)
|
|
50
|
+
{
|
|
51
|
+
std::wstring wstr;
|
|
52
|
+
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
|
|
53
|
+
if (len > 0)
|
|
54
|
+
{
|
|
55
|
+
wstr.resize(len - 1);
|
|
56
|
+
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], len);
|
|
57
|
+
}
|
|
58
|
+
return wstr;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
std::string WindowsPrinter::WideToUtf8(LPWSTR wstr)
|
|
62
|
+
{
|
|
63
|
+
if (!wstr)
|
|
64
|
+
return "";
|
|
65
|
+
|
|
66
|
+
int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
|
|
67
|
+
if (len <= 0)
|
|
68
|
+
return "";
|
|
69
|
+
|
|
70
|
+
std::vector<char> buffer(len);
|
|
71
|
+
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, buffer.data(), len, NULL, NULL);
|
|
72
|
+
return std::string(buffer.data());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
PrinterInfo WindowsPrinter::GetPrinterDetails(const std::string &printerName, bool isDefault)
|
|
76
|
+
{
|
|
77
|
+
PrinterInfo info;
|
|
78
|
+
info.name = printerName;
|
|
79
|
+
info.isDefault = isDefault;
|
|
80
|
+
|
|
81
|
+
HANDLE hPrinter;
|
|
82
|
+
std::wstring wPrinterName = Utf8ToWide(printerName);
|
|
83
|
+
|
|
84
|
+
if (OpenPrinterW((LPWSTR)wPrinterName.c_str(), &hPrinter, NULL))
|
|
85
|
+
{
|
|
86
|
+
DWORD needed;
|
|
87
|
+
GetPrinterW(hPrinter, 2, NULL, 0, &needed);
|
|
88
|
+
if (needed > 0)
|
|
89
|
+
{
|
|
90
|
+
std::vector<BYTE> buffer(needed);
|
|
91
|
+
if (GetPrinterW(hPrinter, 2, buffer.data(), needed, &needed))
|
|
92
|
+
{
|
|
93
|
+
PRINTER_INFO_2W *pInfo = (PRINTER_INFO_2W *)buffer.data();
|
|
94
|
+
info.status = GetPrinterStatus(pInfo->Status);
|
|
95
|
+
|
|
96
|
+
if (pInfo->pLocation)
|
|
97
|
+
info.details["location"] = WideToUtf8(pInfo->pLocation);
|
|
98
|
+
if (pInfo->pComment)
|
|
99
|
+
info.details["comment"] = WideToUtf8(pInfo->pComment);
|
|
100
|
+
if (pInfo->pDriverName)
|
|
101
|
+
info.details["driver"] = WideToUtf8(pInfo->pDriverName);
|
|
102
|
+
if (pInfo->pPortName)
|
|
103
|
+
info.details["port"] = WideToUtf8(pInfo->pPortName);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
ClosePrinter(hPrinter);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return info;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
std::vector<PrinterInfo> WindowsPrinter::GetPrinters()
|
|
113
|
+
{
|
|
114
|
+
std::vector<PrinterInfo> printers;
|
|
115
|
+
DWORD needed = 0, returned = 0;
|
|
116
|
+
|
|
117
|
+
EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &returned);
|
|
118
|
+
if (needed > 0)
|
|
119
|
+
{
|
|
120
|
+
std::vector<BYTE> buffer(needed);
|
|
121
|
+
if (EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 2, buffer.data(), needed, &needed, &returned))
|
|
122
|
+
{
|
|
123
|
+
PRINTER_INFO_2W *pInfo = (PRINTER_INFO_2W *)buffer.data();
|
|
124
|
+
for (DWORD i = 0; i < returned; i++)
|
|
125
|
+
{
|
|
126
|
+
std::string name = WideToUtf8(pInfo[i].pPrinterName);
|
|
127
|
+
bool isDefault = false;
|
|
128
|
+
|
|
129
|
+
wchar_t defaultPrinter[256];
|
|
130
|
+
DWORD size = sizeof(defaultPrinter) / sizeof(defaultPrinter[0]);
|
|
131
|
+
if (GetDefaultPrinterW(defaultPrinter, &size))
|
|
132
|
+
{
|
|
133
|
+
isDefault = (wcscmp(pInfo[i].pPrinterName, defaultPrinter) == 0);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
printers.push_back(GetPrinterDetails(name, isDefault));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return printers;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
PrinterInfo WindowsPrinter::GetSystemDefaultPrinter()
|
|
145
|
+
{
|
|
146
|
+
wchar_t printerName[256];
|
|
147
|
+
DWORD size = sizeof(printerName) / sizeof(printerName[0]);
|
|
148
|
+
|
|
149
|
+
if (GetDefaultPrinterW(printerName, &size))
|
|
150
|
+
{
|
|
151
|
+
std::string name = WideToUtf8(printerName);
|
|
152
|
+
return GetPrinterDetails(name, true);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return PrinterInfo();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
bool WindowsPrinter::PrintDirect(const std::string &printerName, const std::vector<uint8_t> &data, const std::string &dataType)
|
|
159
|
+
{
|
|
160
|
+
HANDLE hPrinter;
|
|
161
|
+
std::wstring wPrinterName = Utf8ToWide(printerName);
|
|
162
|
+
std::wstring wDataType = Utf8ToWide(dataType);
|
|
163
|
+
|
|
164
|
+
if (!OpenPrinterW((LPWSTR)wPrinterName.c_str(), &hPrinter, NULL))
|
|
165
|
+
{
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
DOC_INFO_1W docInfo;
|
|
170
|
+
wchar_t docName[] = L"Node.js Print Job";
|
|
171
|
+
docInfo.pDocName = docName;
|
|
172
|
+
docInfo.pOutputFile = NULL;
|
|
173
|
+
docInfo.pDatatype = const_cast<LPWSTR>(wDataType.c_str());
|
|
174
|
+
|
|
175
|
+
if (StartDocPrinterW(hPrinter, 1, (LPBYTE)&docInfo))
|
|
176
|
+
{
|
|
177
|
+
if (StartPagePrinter(hPrinter))
|
|
178
|
+
{
|
|
179
|
+
DWORD bytesWritten;
|
|
180
|
+
void *buffer = const_cast<void *>(static_cast<const void *>(data.data()));
|
|
181
|
+
if (WritePrinter(hPrinter, buffer, static_cast<DWORD>(data.size()), &bytesWritten))
|
|
182
|
+
{
|
|
183
|
+
EndPagePrinter(hPrinter);
|
|
184
|
+
EndDocPrinter(hPrinter);
|
|
185
|
+
ClosePrinter(hPrinter);
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
EndDocPrinter(hPrinter);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
ClosePrinter(hPrinter);
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
PrinterInfo WindowsPrinter::GetStatusPrinter(const std::string &printerName)
|
|
197
|
+
{
|
|
198
|
+
wchar_t defaultPrinter[256];
|
|
199
|
+
DWORD size = sizeof(defaultPrinter) / sizeof(defaultPrinter[0]);
|
|
200
|
+
bool isDefault = false;
|
|
201
|
+
|
|
202
|
+
if (GetDefaultPrinterW(defaultPrinter, &size))
|
|
203
|
+
{
|
|
204
|
+
std::string defaultPrinterName = WideToUtf8(defaultPrinter);
|
|
205
|
+
isDefault = (printerName == defaultPrinterName);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
PrinterInfo printer = GetPrinterDetails(printerName, isDefault);
|
|
209
|
+
return printer;
|
|
210
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#ifndef WINDOWS_PRINTER_H
|
|
2
|
+
#define WINDOWS_PRINTER_H
|
|
3
|
+
|
|
4
|
+
#define NOMINMAX
|
|
5
|
+
#include <windows.h>
|
|
6
|
+
#include <winspool.h>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include "printer_interface.h"
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
// Desabilita todas as macros do Windows que podem interferir
|
|
12
|
+
#ifdef GetDefaultPrinter
|
|
13
|
+
#undef GetDefaultPrinter
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
#ifdef GetPrinter
|
|
17
|
+
#undef GetPrinter
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
class WindowsPrinter : public PrinterInterface
|
|
21
|
+
{
|
|
22
|
+
private:
|
|
23
|
+
std::string GetPrinterStatus(DWORD status);
|
|
24
|
+
std::wstring Utf8ToWide(const std::string &str);
|
|
25
|
+
std::string WideToUtf8(LPWSTR wstr);
|
|
26
|
+
|
|
27
|
+
public:
|
|
28
|
+
virtual PrinterInfo GetPrinterDetails(const std::string &printerName, bool isDefault = false) override;
|
|
29
|
+
virtual std::vector<PrinterInfo> GetPrinters() override;
|
|
30
|
+
virtual PrinterInfo GetSystemDefaultPrinter() override;
|
|
31
|
+
virtual bool PrintDirect(const std::string &printerName, const std::vector<uint8_t> &data, const std::string &dataType) override;
|
|
32
|
+
virtual PrinterInfo GetStatusPrinter(const std::string &printerName) override;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
#endif
|
package/.gitattributes
DELETED
package/Gruntfile.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
module.exports = function(grunt) {
|
|
2
|
-
grunt.initConfig({
|
|
3
|
-
shell: {
|
|
4
|
-
'node-pre-gyp-ia32': {
|
|
5
|
-
command: 'node-pre-gyp configure build package --target_arch=ia32'
|
|
6
|
-
},
|
|
7
|
-
'node-pre-gyp-x64': {
|
|
8
|
-
command: 'node-pre-gyp configure build package --target_arch=x64'
|
|
9
|
-
},
|
|
10
|
-
'node-gyp-ia32': {
|
|
11
|
-
command: 'node-gyp rebuild --arch=ia32'
|
|
12
|
-
},
|
|
13
|
-
'node-gyp-x64': {
|
|
14
|
-
command: 'node-gyp rebuild --arch=x64'
|
|
15
|
-
},
|
|
16
|
-
'upload-binaries': {
|
|
17
|
-
command: 'node-pre-gyp-github publish'
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
copy: {
|
|
21
|
-
ia32: {
|
|
22
|
-
files: [
|
|
23
|
-
{src: 'build/Release/electron-printer.node', dest: 'lib/electron-printer-' + process.platform + '-ia32.node'},
|
|
24
|
-
{src: 'binding.js', dest: 'lib/binding.js'},
|
|
25
|
-
{src: 'index.js', dest: 'lib/index.js'}
|
|
26
|
-
]
|
|
27
|
-
},
|
|
28
|
-
x64: {
|
|
29
|
-
files: [
|
|
30
|
-
{src: 'build/Release/electron-printer.node', dest: 'lib/electron-printer-' + process.platform + '-x64.node'},
|
|
31
|
-
{src: 'binding.js', dest: 'lib/binding.js'},
|
|
32
|
-
{src: 'index.js', dest: 'lib/index.js'}
|
|
33
|
-
]
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
grunt.loadNpmTasks('grunt-contrib-jshint');
|
|
39
|
-
grunt.loadNpmTasks('grunt-shell');
|
|
40
|
-
grunt.loadNpmTasks('grunt-contrib-copy');
|
|
41
|
-
|
|
42
|
-
grunt.registerTask('build-pre-ia32', [
|
|
43
|
-
'shell:node-pre-gyp-ia32',
|
|
44
|
-
'copy:ia32'
|
|
45
|
-
]);
|
|
46
|
-
|
|
47
|
-
grunt.registerTask('build-ia32', [
|
|
48
|
-
'shell:node-gyp-ia32',
|
|
49
|
-
'copy:ia32'
|
|
50
|
-
]);
|
|
51
|
-
|
|
52
|
-
grunt.registerTask('build-x64', [
|
|
53
|
-
'shell:node-gyp-x64',
|
|
54
|
-
'copy:x64'
|
|
55
|
-
]);
|
|
56
|
-
|
|
57
|
-
grunt.registerTask('build-pre-x64', [
|
|
58
|
-
'shell:node-pre-gyp-x64',
|
|
59
|
-
'copy:x64'
|
|
60
|
-
]);
|
|
61
|
-
|
|
62
|
-
grunt.registerTask('build', [
|
|
63
|
-
'build-ia32',
|
|
64
|
-
'build-x64'
|
|
65
|
-
]);
|
|
66
|
-
|
|
67
|
-
grunt.registerTask('build-pre', [
|
|
68
|
-
'build-pre-ia32',
|
|
69
|
-
'build-pre-x64'
|
|
70
|
-
]);
|
|
71
|
-
|
|
72
|
-
grunt.registerTask('upload', [
|
|
73
|
-
'shell:upload-binaries'
|
|
74
|
-
]);
|
|
75
|
-
|
|
76
|
-
grunt.registerTask('release', [
|
|
77
|
-
'build-pre',
|
|
78
|
-
'upload'
|
|
79
|
-
]);
|
|
80
|
-
};
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 MOHAMMED ESSLASSI
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/binding.js
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
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/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./binding');
|
package/printer.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./lib/binding');
|
package/src/hello_world.cc
DELETED
package/src/macros.hh
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
#ifndef NODE_PRINTER_SRC_MACROS_H
|
|
2
|
-
#define NODE_PRINTER_SRC_MACROS_H
|
|
3
|
-
#include <napi.h>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
#define MY_NODE_MODULE_ENV_DECL Napi::Env env = info.Env();
|
|
7
|
-
#define MY_NODE_MODULE_ENV env
|
|
8
|
-
#define MY_NODE_MODULE_HANDLESCOPE MY_NODE_MODULE_ENV_DECL Napi::HandleScope scope(env);
|
|
9
|
-
|
|
10
|
-
#define MY_MODULE_SET_METHOD(exports, name, method) \
|
|
11
|
-
(exports).Set(Napi::String::New(env, name), Napi::Function::New(env, method))
|
|
12
|
-
|
|
13
|
-
#define MY_NODE_MODULE_CALLBACK(name) Napi::Value name(const Napi::CallbackInfo& info)
|
|
14
|
-
|
|
15
|
-
#define MY_NODE_MODULE_RETURN_VALUE(value) \
|
|
16
|
-
return value;
|
|
17
|
-
|
|
18
|
-
#define NAPI_STRING_NEW_UTF8(env, value) Napi::String::New(env, value)
|
|
19
|
-
|
|
20
|
-
#define ADD_NAPI_NUMBER_PROPERTY(obj, name, value) \
|
|
21
|
-
obj.Set(Napi::String::New(obj.Env(), name), Napi::Number::New(obj.Env(), value))
|
|
22
|
-
|
|
23
|
-
#define NAPI_STRING_NEW_2BYTES(env, value) Napi::String::New(env, (char16_t*)value)
|
|
24
|
-
|
|
25
|
-
#define ADD_NAPI_STRING_PROPERTY(obj, name, key) \
|
|
26
|
-
if ((job->key != NULL) && (*job->key != L'\0')) { \
|
|
27
|
-
obj.Set(Napi::String::New(obj.Env(), name), Napi::String::New(obj.Env(), (char16_t*)job->key)); \
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
#define RETURN_EXCEPTION(env, msg) \
|
|
31
|
-
Napi::Error::New(env, msg).ThrowAsJavaScriptException(); \
|
|
32
|
-
return env.Null(); // Ensure a Napi::Value is returned
|
|
33
|
-
|
|
34
|
-
#define RETURN_EXCEPTION_STR(env, msg) \
|
|
35
|
-
RETURN_EXCEPTION(env, msg)
|
|
36
|
-
|
|
37
|
-
#define REQUIRE_ARGUMENTS(env, args, n) \
|
|
38
|
-
if (args.Length() < (n)) { \
|
|
39
|
-
RETURN_EXCEPTION_STR(env, "Expected " #n " arguments"); \
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
#define ARG_CHECK_STRING(env, args, i) \
|
|
43
|
-
if ((args).Length() <= (i) || !(args)[(i)].IsString()) { \
|
|
44
|
-
RETURN_EXCEPTION_STR(env, "Argument " #i " must be a string"); \
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
#define REQUIRE_ARGUMENT_STRINGW(env, args, i, var) \
|
|
48
|
-
ARG_CHECK_STRING(env, args, i); \
|
|
49
|
-
std::wstring var = (args)[(i)].As<Napi::String>().Utf16Value();
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
#endif
|
|
53
|
-
|
package/src/node_printer.cc
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
#include "node_printer.hpp"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
6
|
-
MY_MODULE_SET_METHOD(exports, "SayMyName", SayMyName);
|
|
7
|
-
MY_MODULE_SET_METHOD(exports, "getPrinters", getPrinters);
|
|
8
|
-
MY_MODULE_SET_METHOD(exports, "getDefaultPrinterName", getDefaultPrinterName);
|
|
9
|
-
MY_MODULE_SET_METHOD(exports, "printDirect", printDirect);
|
|
10
|
-
MY_MODULE_SET_METHOD(exports, "getPrinter", getPrinter);
|
|
11
|
-
MY_MODULE_SET_METHOD(exports, "printFile", printFile);
|
|
12
|
-
MY_MODULE_SET_METHOD(exports, "getSupportedPrintFormats", getSupportedPrintFormats);
|
|
13
|
-
return exports;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
NODE_API_MODULE(addon, Init)
|
|
17
|
-
|
|
18
|
-
// Helpers
|
|
19
|
-
|
|
20
|
-
bool getStringOrBufferFromNapiValue(const Napi::Value& value, std::string& oData) {
|
|
21
|
-
if (value.IsString()) {
|
|
22
|
-
oData = value.As<Napi::String>().Utf8Value();
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
if (value.IsBuffer()) {
|
|
26
|
-
Napi::Buffer<char> buffer = value.As<Napi::Buffer<char>>();
|
|
27
|
-
oData.assign(buffer.Data(), buffer.Length());
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|