@esslassi/electron-printer 0.0.1
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/.gitattributes +2 -0
- package/Gruntfile.js +80 -0
- package/LICENSE +21 -0
- package/README.md +82 -0
- package/binding.gyp +39 -0
- package/binding.js +244 -0
- package/index.js +1 -0
- package/package.json +49 -0
- package/printer.js +1 -0
- package/src/hello_world.cc +8 -0
- package/src/macros.hh +53 -0
- package/src/node_printer.cc +31 -0
- package/src/node_printer.hpp +77 -0
- package/src/node_printer_win.cc +586 -0
- package/test/getDefaultPrinterName.test.js +18 -0
- package/test/getPrinters.test.js +26 -0
- package/test/incompleteFunctions.js +56 -0
- package/test/printDirect.test.js +34 -0
- package/test/sayMyName.test.js +21 -0
- package/tools/buildElectronLinux.sh +10 -0
- package/tools/buildElectronWindows.ps1 +20 -0
- package/tools/buildWindows.ps1 +23 -0
- package/tools/generateReleaseBuildsLinux.sh +59 -0
- package/tools/generateReleaseBuildsWindows.ps1 +63 -0
- package/tools/getSourceFiles.py +12 -0
- package/tools/remove_directory.py +27 -0
- package/tsconfig.json +22 -0
- package/types/index.d.ts +56 -0
package/.gitattributes
ADDED
package/Gruntfile.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# @esslassi/electron-printer
|
|
2
|
+
|
|
3
|
+
**No recompilation required when upgrading Node.js versions, thanks to N-API!** 🎉
|
|
4
|
+
|
|
5
|
+
Native bind printers on POSIX and Windows OS from Node.js, Electron, and node-webkit.
|
|
6
|
+
|
|
7
|
+
!npm version !Prebuild Binaries and Publish
|
|
8
|
+
|
|
9
|
+
> Supports Node.js versions from 8.0.0 onwards, including the latest versions, thanks to the transition to N-API.
|
|
10
|
+
|
|
11
|
+
> Prebuild and CI integration courtesy of @ekoeryanto in his FORK
|
|
12
|
+
|
|
13
|
+
If you have a problem, ask a question on !Gitter or find/create a new Github issue
|
|
14
|
+
|
|
15
|
+
___
|
|
16
|
+
### **Below is the original README**
|
|
17
|
+
___
|
|
18
|
+
### Reason:
|
|
19
|
+
|
|
20
|
+
I was involved in a project where I needed to print from Node.js. This is the reason why I created this project and I want to share my code with others.
|
|
21
|
+
|
|
22
|
+
### Features:
|
|
23
|
+
|
|
24
|
+
* No dependencies on NAN or V8;
|
|
25
|
+
* Native method wrappers for Windows and POSIX (which uses CUPS 1.4/MAC OS X 10.6) APIs;
|
|
26
|
+
* Compatible with Node.js versions that support N-API, ensuring long-term stability and compatibility;
|
|
27
|
+
* Compatible with Electron versions that support N-API, reducing the need for frequent recompilation;
|
|
28
|
+
* `getPrinters()` to enumerate all installed printers with current jobs and statuses;
|
|
29
|
+
* `getPrinter(printerName)` to get a specific/default printer info with current jobs and statuses;
|
|
30
|
+
* `getPrinterDriverOptions(printerName)` (POSIX only) to get a specific/default printer driver options such as supported paper size and other info;
|
|
31
|
+
* `getSelectedPaperSize(printerName)` (POSIX only) to get a specific/default printer default paper size from its driver options;
|
|
32
|
+
* `getDefaultPrinterName()` returns the default printer name;
|
|
33
|
+
* `printDirect(options)` to send a job to a specific/default printer, now supports CUPS options passed in the form of a JS object (see `cancelJob.js` example). To print a PDF from Windows, it is possible by using node-pdfium module to convert a PDF format into EMF and then send it to the printer as EMF;
|
|
34
|
+
* `printFile(options)` (POSIX only) to print a file;
|
|
35
|
+
* `getSupportedPrintFormats()` to get all possible print formats for the `printDirect` method, which depends on the OS. `RAW` and `TEXT` are supported on all OSes;
|
|
36
|
+
* `getJob(printerName, jobId)` to get specific job info including job status;
|
|
37
|
+
* `setJob(printerName, jobId, command)` to send a command to a job (e.g., `'CANCEL'` to cancel the job);
|
|
38
|
+
* `getSupportedJobCommands()` to get supported job commands for `setJob()`, depending on the OS. The `'CANCEL'` command is supported on all OSes.
|
|
39
|
+
|
|
40
|
+
### How to install:
|
|
41
|
+
```
|
|
42
|
+
npm install @esslassi/electron-printer
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### How to use:
|
|
46
|
+
|
|
47
|
+
See examples
|
|
48
|
+
|
|
49
|
+
### Author(s):
|
|
50
|
+
|
|
51
|
+
* Mohammed Esslassi, contact@foxneer.com
|
|
52
|
+
|
|
53
|
+
### Node.js Version Support:
|
|
54
|
+
|
|
55
|
+
This project supports Node.js versions from 8.0.0 onwards. N-API ensures that native addons do not require recompilation when upgrading Node.js versions. For more details, refer to the [Node.js N-API documentation](https://nodejs.org/api/n-api.html)¹(https://nodejs.org/api/n-api.html).
|
|
56
|
+
|
|
57
|
+
Feel free to download, test, and propose new features.
|
|
58
|
+
|
|
59
|
+
### License:
|
|
60
|
+
The MIT License (MIT)
|
|
61
|
+
|
|
62
|
+
## Contributing
|
|
63
|
+
|
|
64
|
+
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
This project is licensed under the MIT License.
|
|
69
|
+
|
|
70
|
+
### Keywords:
|
|
71
|
+
|
|
72
|
+
* node-printer
|
|
73
|
+
* printer
|
|
74
|
+
* electron-printer
|
|
75
|
+
* node-addon-api
|
|
76
|
+
* POSIX printer
|
|
77
|
+
* Windows printer
|
|
78
|
+
* CUPS printer
|
|
79
|
+
* print job
|
|
80
|
+
* printer driver
|
|
81
|
+
|
|
82
|
+
---
|
package/binding.gyp
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
'variables': {
|
|
3
|
+
'module_name%': 'electron-printer',
|
|
4
|
+
'module_path%': 'lib'
|
|
5
|
+
},
|
|
6
|
+
'targets': [
|
|
7
|
+
{
|
|
8
|
+
"target_name": "action_after_build",
|
|
9
|
+
"type": "none",
|
|
10
|
+
"dependencies": [ "<(module_name)" ],
|
|
11
|
+
"copies": [
|
|
12
|
+
{
|
|
13
|
+
"files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
|
|
14
|
+
"destination": "<(module_path)"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
'target_name': '<(module_name)',
|
|
20
|
+
'sources': [
|
|
21
|
+
# is like "ls -1 src/*.cc", but gyp does not support direct patterns on
|
|
22
|
+
# sources
|
|
23
|
+
'<!@(["python", "tools/getSourceFiles.py", "src", "cc"])'
|
|
24
|
+
],
|
|
25
|
+
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
|
|
26
|
+
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
|
|
27
|
+
'cflags!': [ '-fno-exceptions' ],
|
|
28
|
+
'cflags_cc!': [ '-fno-exceptions' ],
|
|
29
|
+
'xcode_settings': {
|
|
30
|
+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
|
|
31
|
+
'CLANG_CXX_LIBRARY': 'libc++',
|
|
32
|
+
'MACOSX_DEPLOYMENT_TARGET': '10.7'
|
|
33
|
+
},
|
|
34
|
+
'msvs_settings': {
|
|
35
|
+
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
}
|
package/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/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./binding');
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@esslassi/electron-printer",
|
|
3
|
+
"description": "Node API (N-API) supported electron.s || node.js printer bindings",
|
|
4
|
+
"homepage": "https://github.com/esslassi/electron-printer",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "types/index.d.ts",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"node-addon-api": "^8.1.0"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"install": "prebuild-install || node-gyp rebuild",
|
|
13
|
+
"rebuild": "node-gyp rebuild",
|
|
14
|
+
"release": "node-pre-gyp-github publish --release --commitish main",
|
|
15
|
+
"electron-rebuild": "node-gyp rebuild --target_platform=win32 --target_arch=x64 --runtime=electron --target=35.0.1 --dist-url=https://electronjs.org/headers",
|
|
16
|
+
"test": "node --test test/"
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
"author": {
|
|
20
|
+
"name": "Esslassi Mohammed",
|
|
21
|
+
"url": "http://foxneer.com/",
|
|
22
|
+
"email": "contact@foxneer.com"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/esslassi/electron-printer.git"
|
|
27
|
+
},
|
|
28
|
+
"binary": {
|
|
29
|
+
"module_name": "electron-printer",
|
|
30
|
+
"module_path": "./lib/",
|
|
31
|
+
"host": "https://github.com/esslassi/electron-printer/releases/download/",
|
|
32
|
+
"remote_path": "v{version}",
|
|
33
|
+
"tag_path": "https://github.com/esslassi/electron-printer/releases/tag/v{version}"
|
|
34
|
+
},
|
|
35
|
+
"licenses": [
|
|
36
|
+
{
|
|
37
|
+
"type": "BSD"
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@mapbox/node-pre-gyp": "^2.0.0",
|
|
42
|
+
"grunt": "^1.6.1",
|
|
43
|
+
"grunt-contrib-copy": "^1.0.0",
|
|
44
|
+
"grunt-contrib-jshint": "^3.2.0",
|
|
45
|
+
"grunt-node-gyp": "^5.0.0",
|
|
46
|
+
"grunt-shell": "^4.0.0",
|
|
47
|
+
"node-pre-gyp-github": "^2.0.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/printer.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/binding');
|
package/src/macros.hh
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
}
|