@esslassi/electron-printer 0.0.3 → 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/lib/binding.js +0 -244
- package/lib/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
package/README.md
CHANGED
|
@@ -1,82 +1,251 @@
|
|
|
1
|
-
|
|
1
|
+
Here is the clean **README.md** content ready to copy and paste:
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Electron Printer (@esslassi/electron-printer)
|
|
6
|
+
|
|
7
|
+
Node.js and Electron bindings for printer management and direct printing. Supports Windows and Linux (CUPS).
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
## Features
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
* List all available printers
|
|
12
|
+
* Get the system default printer
|
|
13
|
+
* Get detailed printer status
|
|
14
|
+
* Direct printing (raw printing)
|
|
15
|
+
* TypeScript support
|
|
16
|
+
* Asynchronous API (Promises)
|
|
17
|
+
* Compatible with Node.js and Electron
|
|
8
18
|
|
|
9
|
-
|
|
19
|
+
## Requirements
|
|
10
20
|
|
|
11
|
-
|
|
21
|
+
* Node.js >= 18.20.6
|
|
22
|
+
* Electron >= 20.0.0
|
|
23
|
+
* Windows or Linux
|
|
24
|
+
* For Windows: Visual Studio Build Tools
|
|
25
|
+
* For Linux: CUPS development headers
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
sudo apt-get install libcups2-dev
|
|
29
|
+
```
|
|
12
30
|
|
|
13
|
-
|
|
31
|
+
## Installation
|
|
14
32
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
### Reason:
|
|
33
|
+
```bash
|
|
34
|
+
npm install @esslassi/electron-printer
|
|
35
|
+
```
|
|
19
36
|
|
|
20
|
-
|
|
37
|
+
For development:
|
|
21
38
|
|
|
22
|
-
|
|
39
|
+
```bash
|
|
40
|
+
git clone https://github.com/esslassi/electron-printer.git
|
|
41
|
+
cd electron-printer
|
|
42
|
+
npm install
|
|
43
|
+
npm run rebuild
|
|
44
|
+
```
|
|
23
45
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### JavaScript
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
const printer = require('@esslassi/electron-printer');
|
|
52
|
+
|
|
53
|
+
// List all printers
|
|
54
|
+
printer.getPrinters()
|
|
55
|
+
.then(printers => {
|
|
56
|
+
console.log('Available printers:', printers);
|
|
57
|
+
})
|
|
58
|
+
.catch(console.error);
|
|
59
|
+
|
|
60
|
+
// Get default printer
|
|
61
|
+
printer.getDefaultPrinter()
|
|
62
|
+
.then(defaultPrinter => {
|
|
63
|
+
console.log('Default printer:', defaultPrinter);
|
|
64
|
+
})
|
|
65
|
+
.catch(console.error);
|
|
66
|
+
|
|
67
|
+
// Check printer status
|
|
68
|
+
printer.getStatusPrinter({ printerName: 'Printer Name' })
|
|
69
|
+
.then(status => {
|
|
70
|
+
console.log('Printer status:', status);
|
|
71
|
+
})
|
|
72
|
+
.catch(console.error);
|
|
73
|
+
|
|
74
|
+
// Print directly
|
|
75
|
+
const printOptions = {
|
|
76
|
+
printerName: 'Printer Name',
|
|
77
|
+
data: 'Text to print',
|
|
78
|
+
dataType: 'RAW' // optional (default is 'RAW')
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
printer.printDirect(printOptions)
|
|
82
|
+
.then(result => {
|
|
83
|
+
console.log('Result:', result);
|
|
84
|
+
})
|
|
85
|
+
.catch(console.error);
|
|
86
|
+
```
|
|
39
87
|
|
|
40
|
-
###
|
|
88
|
+
### TypeScript
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import printer, {
|
|
92
|
+
Printer,
|
|
93
|
+
PrintDirectOptions,
|
|
94
|
+
GetStatusPrinterOptions
|
|
95
|
+
} from '@esslassi/electron-printer';
|
|
96
|
+
|
|
97
|
+
async function example() {
|
|
98
|
+
try {
|
|
99
|
+
const printers: Printer[] = await printer.getPrinters();
|
|
100
|
+
console.log('Printers:', printers);
|
|
101
|
+
|
|
102
|
+
const defaultPrinter: Printer = await printer.getDefaultPrinter();
|
|
103
|
+
console.log('Default printer:', defaultPrinter);
|
|
104
|
+
|
|
105
|
+
const statusOptions: GetStatusPrinterOptions = {
|
|
106
|
+
printerName: 'Printer Name'
|
|
107
|
+
};
|
|
108
|
+
const status: Printer = await printer.getStatusPrinter(statusOptions);
|
|
109
|
+
console.log('Status:', status);
|
|
110
|
+
|
|
111
|
+
const printOptions: PrintDirectOptions = {
|
|
112
|
+
printerName: 'Printer Name',
|
|
113
|
+
data: Buffer.from('Text to print'),
|
|
114
|
+
dataType: 'RAW'
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const result = await printer.printDirect(printOptions);
|
|
118
|
+
console.log('Result:', result);
|
|
119
|
+
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error('Error:', error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
41
124
|
```
|
|
42
|
-
|
|
125
|
+
|
|
126
|
+
## API
|
|
127
|
+
|
|
128
|
+
### getPrinters(): Promise<Printer[]>
|
|
129
|
+
|
|
130
|
+
Lists all printers installed on the system.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
interface Printer {
|
|
134
|
+
name: string;
|
|
135
|
+
isDefault: boolean;
|
|
136
|
+
status: string;
|
|
137
|
+
details: {
|
|
138
|
+
location?: string;
|
|
139
|
+
comment?: string;
|
|
140
|
+
driver?: string;
|
|
141
|
+
port?: string;
|
|
142
|
+
[key: string]: string | undefined;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
43
145
|
```
|
|
44
146
|
|
|
45
|
-
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
### getDefaultPrinter(): Promise<Printer>
|
|
46
150
|
|
|
47
|
-
|
|
151
|
+
Returns the system default printer.
|
|
152
|
+
|
|
153
|
+
---
|
|
48
154
|
|
|
49
|
-
###
|
|
155
|
+
### getStatusPrinter(options: GetStatusPrinterOptions): Promise<Printer>
|
|
50
156
|
|
|
51
|
-
|
|
157
|
+
```typescript
|
|
158
|
+
interface GetStatusPrinterOptions {
|
|
159
|
+
printerName: string;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
52
162
|
|
|
53
|
-
|
|
163
|
+
---
|
|
54
164
|
|
|
55
|
-
|
|
165
|
+
### printDirect(options: PrintDirectOptions): Promise<string>
|
|
56
166
|
|
|
57
|
-
|
|
167
|
+
```typescript
|
|
168
|
+
interface PrintDirectOptions {
|
|
169
|
+
printerName: string;
|
|
170
|
+
data: string | Buffer;
|
|
171
|
+
dataType?: 'RAW' | 'TEXT' | 'COMMAND' | 'AUTO';
|
|
172
|
+
}
|
|
173
|
+
```
|
|
58
174
|
|
|
59
|
-
###
|
|
60
|
-
|
|
175
|
+
### Possible Status Values
|
|
176
|
+
|
|
177
|
+
* ready
|
|
178
|
+
* offline
|
|
179
|
+
* error
|
|
180
|
+
* paper-jam
|
|
181
|
+
* paper-out
|
|
182
|
+
* manual-feed
|
|
183
|
+
* paper-problem
|
|
184
|
+
* busy
|
|
185
|
+
* printing
|
|
186
|
+
* output-bin-full
|
|
187
|
+
* not-available
|
|
188
|
+
* waiting
|
|
189
|
+
* processing
|
|
190
|
+
* initializing
|
|
191
|
+
* warming-up
|
|
192
|
+
* toner-low
|
|
193
|
+
* no-toner
|
|
194
|
+
* page-punt
|
|
195
|
+
* user-intervention
|
|
196
|
+
* out-of-memory
|
|
197
|
+
* door-open
|
|
61
198
|
|
|
62
|
-
|
|
199
|
+
---
|
|
63
200
|
|
|
64
|
-
|
|
201
|
+
## Supported Platforms
|
|
65
202
|
|
|
66
|
-
|
|
203
|
+
* Windows (32/64-bit)
|
|
204
|
+
* Linux (CUPS)
|
|
67
205
|
|
|
68
|
-
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Troubleshooting
|
|
209
|
+
|
|
210
|
+
### Windows
|
|
211
|
+
|
|
212
|
+
1. Install Visual Studio Build Tools
|
|
213
|
+
2. Run:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
npm run rebuild
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
3. Verify printer access permissions
|
|
220
|
+
|
|
221
|
+
### Linux
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
sudo apt-get install libcups2-dev
|
|
225
|
+
sudo service cups status
|
|
226
|
+
sudo usermod -a -G lp $USER
|
|
227
|
+
```
|
|
69
228
|
|
|
70
|
-
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## Development
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
git clone https://github.com/esslassi/electron-printer.git
|
|
235
|
+
cd electron-printer
|
|
236
|
+
npm install
|
|
237
|
+
npm run rebuild
|
|
238
|
+
node test.js
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## License
|
|
71
244
|
|
|
72
|
-
|
|
73
|
-
* printer
|
|
74
|
-
* electron-printer
|
|
75
|
-
* node-addon-api
|
|
76
|
-
* POSIX printer
|
|
77
|
-
* Windows printer
|
|
78
|
-
* CUPS printer
|
|
79
|
-
* print job
|
|
80
|
-
* printer driver
|
|
245
|
+
MIT
|
|
81
246
|
|
|
82
247
|
---
|
|
248
|
+
|
|
249
|
+
## Author
|
|
250
|
+
|
|
251
|
+
Esslassi
|
package/binding.gyp
CHANGED
|
@@ -1,39 +1,57 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
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
|
-
},
|
|
2
|
+
"targets": [
|
|
18
3
|
{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
4
|
+
"target_name": "electron_printer",
|
|
5
|
+
"sources": [
|
|
6
|
+
"src/main.cpp",
|
|
7
|
+
"src/print.cpp",
|
|
8
|
+
"src/printer_factory.cpp"
|
|
9
|
+
],
|
|
10
|
+
"include_dirs": [
|
|
11
|
+
"<!@(node -p \"require('node-addon-api').include\")"
|
|
24
12
|
],
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
'
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
13
|
+
"dependencies": [
|
|
14
|
+
"<!(node -p \"require('node-addon-api').gyp\")"
|
|
15
|
+
],
|
|
16
|
+
"defines": [ "NAPI_CPP_EXCEPTIONS" ],
|
|
17
|
+
"conditions": [
|
|
18
|
+
['OS=="win"', {
|
|
19
|
+
"sources": ["src/windows_printer.cpp"],
|
|
20
|
+
"libraries": ["winspool.lib"],
|
|
21
|
+
"msvs_settings": {
|
|
22
|
+
"VCCLCompilerTool": {
|
|
23
|
+
"ExceptionHandling": 1
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}],
|
|
27
|
+
['OS=="mac"', {
|
|
28
|
+
"sources": ["src/mac_printer.cpp"],
|
|
29
|
+
"libraries": ["-lcups"],
|
|
30
|
+
"include_dirs": [
|
|
31
|
+
"/usr/include/cups"
|
|
32
|
+
],
|
|
33
|
+
"xcode_settings": {
|
|
34
|
+
"OTHER_CFLAGS": ["-Wall"],
|
|
35
|
+
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
|
|
36
|
+
"CLANG_CXX_LIBRARY": "libc++",
|
|
37
|
+
"MACOSX_DEPLOYMENT_TARGET": "10.7"
|
|
38
|
+
}
|
|
39
|
+
}],
|
|
40
|
+
['OS=="linux"', {
|
|
41
|
+
"sources": ["src/linux_printer.cpp"],
|
|
42
|
+
"libraries": ["-lcups"],
|
|
43
|
+
"include_dirs": [
|
|
44
|
+
"/usr/include/cups"
|
|
45
|
+
],
|
|
46
|
+
"cflags": [
|
|
47
|
+
"-Wall",
|
|
48
|
+
"-fexceptions"
|
|
49
|
+
],
|
|
50
|
+
"cflags_cc": [
|
|
51
|
+
"-fexceptions"
|
|
52
|
+
]
|
|
53
|
+
}]
|
|
54
|
+
]
|
|
37
55
|
}
|
|
38
56
|
]
|
|
39
57
|
}
|
package/package.json
CHANGED
|
@@ -1,48 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esslassi/electron-printer",
|
|
3
|
-
"description": "Node
|
|
4
|
-
"
|
|
5
|
-
"version": "0.0.3",
|
|
3
|
+
"description": "Node.js and Electron bindings",
|
|
4
|
+
"version": "0.0.5",
|
|
6
5
|
"main": "./lib/index.js",
|
|
7
|
-
"
|
|
8
|
-
"dependencies": {
|
|
9
|
-
"node-addon-api": "^8.1.0"
|
|
10
|
-
},
|
|
6
|
+
"private": false,
|
|
11
7
|
"scripts": {
|
|
12
|
-
"install": "
|
|
8
|
+
"install": "node-gyp rebuild",
|
|
9
|
+
"prebuild:master": "prebuild-install || node-gyp rebuild",
|
|
10
|
+
"clean:lib": "rimraf lib/ && rimraf tsconfig-build.tsbuildinfo",
|
|
11
|
+
"build": "npm run clean:lib && tsc -p tsconfig-build.json && node-gyp build",
|
|
13
12
|
"rebuild": "node-gyp rebuild",
|
|
14
|
-
"release": "node
|
|
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
|
-
"author": {
|
|
19
|
-
"name": "Esslassi Mohammed",
|
|
20
|
-
"url": "http://foxneer.com/",
|
|
21
|
-
"email": "contact@foxneer.com"
|
|
13
|
+
"release": "node release.js"
|
|
22
14
|
},
|
|
23
15
|
"repository": {
|
|
24
16
|
"type": "git",
|
|
25
|
-
"url": "
|
|
17
|
+
"url": "git@github.com:esslassi/electron-printer.git"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/esslassi/electron-printer/#readme",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"native",
|
|
22
|
+
"electron",
|
|
23
|
+
"node",
|
|
24
|
+
"printer"
|
|
25
|
+
],
|
|
26
|
+
"author": {
|
|
27
|
+
"name": "Esslassi Mohammed",
|
|
28
|
+
"url": "https://github.com/esslassi/electron-printer/#readme",
|
|
29
|
+
"email": "esslassi1996@gmail.com"
|
|
26
30
|
},
|
|
27
31
|
"binary": {
|
|
28
|
-
"module_name": "
|
|
29
|
-
"module_path": "
|
|
32
|
+
"module_name": "electron_printer",
|
|
33
|
+
"module_path": "build/Release/",
|
|
30
34
|
"host": "https://github.com/esslassi/electron-printer/releases/download/",
|
|
31
|
-
"remote_path": "v{version}"
|
|
32
|
-
|
|
35
|
+
"remote_path": "v{version}"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"gypfile": true,
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"bindings": "1.5.0",
|
|
41
|
+
"dotenv": "17.2.3",
|
|
42
|
+
"node-addon-api": "8.5.0",
|
|
43
|
+
"node-gyp": "12.1.0"
|
|
33
44
|
},
|
|
34
|
-
"licenses": [
|
|
35
|
-
{
|
|
36
|
-
"type": "BSD"
|
|
37
|
-
}
|
|
38
|
-
],
|
|
39
45
|
"devDependencies": {
|
|
40
|
-
"@
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
46
|
+
"@semantic-release/changelog": "6.0.3",
|
|
47
|
+
"@semantic-release/commit-analyzer": "13.0.1",
|
|
48
|
+
"@semantic-release/git": "10.0.1",
|
|
49
|
+
"@semantic-release/github": "11.0.1",
|
|
50
|
+
"@semantic-release/npm": "12.0.1",
|
|
51
|
+
"@semantic-release/release-notes-generator": "14.0.3",
|
|
52
|
+
"@tsconfig/node18": "^18.2.4",
|
|
53
|
+
"@types/bindings": "^1.5.5",
|
|
54
|
+
"@types/node": "22.12.0",
|
|
55
|
+
"prebuild-install": "7.1.3",
|
|
56
|
+
"rimraf": "^6.0.1",
|
|
57
|
+
"typescript": "5.7.3"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">= 18.x"
|
|
47
61
|
}
|
|
48
62
|
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
#include "linux_printer.h"
|
|
2
|
+
#include <cups/cups.h>
|
|
3
|
+
#include <cups/ppd.h>
|
|
4
|
+
|
|
5
|
+
std::string LinuxPrinter::GetPrinterStatus(ipp_pstate_t state)
|
|
6
|
+
{
|
|
7
|
+
switch (state)
|
|
8
|
+
{
|
|
9
|
+
case IPP_PRINTER_IDLE:
|
|
10
|
+
return "ready";
|
|
11
|
+
case IPP_PRINTER_PROCESSING:
|
|
12
|
+
return "printing";
|
|
13
|
+
case IPP_PRINTER_STOPPED:
|
|
14
|
+
return "stopped";
|
|
15
|
+
default:
|
|
16
|
+
return "unknown";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
PrinterInfo LinuxPrinter::GetPrinterDetails(const std::string &printerName, bool isDefault)
|
|
21
|
+
{
|
|
22
|
+
PrinterInfo info;
|
|
23
|
+
info.name = printerName;
|
|
24
|
+
info.isDefault = isDefault;
|
|
25
|
+
|
|
26
|
+
cups_dest_t *dests;
|
|
27
|
+
int num_dests = cupsGetDests(&dests);
|
|
28
|
+
cups_dest_t *dest = cupsGetDest(printerName.c_str(), NULL, num_dests, dests);
|
|
29
|
+
|
|
30
|
+
if (dest != NULL)
|
|
31
|
+
{
|
|
32
|
+
for (int i = 0; i < dest->num_options; i++)
|
|
33
|
+
{
|
|
34
|
+
info.details[dest->options[i].name] = dest->options[i].value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
http_t *http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
|
|
38
|
+
HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
|
|
39
|
+
if (http != NULL)
|
|
40
|
+
{
|
|
41
|
+
ipp_t *request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
|
|
42
|
+
|
|
43
|
+
char uri[HTTP_MAX_URI];
|
|
44
|
+
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
|
|
45
|
+
"localhost", 0, "/printers/%s", printerName.c_str());
|
|
46
|
+
|
|
47
|
+
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|
48
|
+
"printer-uri", NULL, uri);
|
|
49
|
+
|
|
50
|
+
ipp_t *response = cupsDoRequest(http, request, "/");
|
|
51
|
+
if (response != NULL)
|
|
52
|
+
{
|
|
53
|
+
ipp_attribute_t *attr = ippFindAttribute(response,
|
|
54
|
+
"printer-state", IPP_TAG_ENUM);
|
|
55
|
+
if (attr != NULL)
|
|
56
|
+
{
|
|
57
|
+
info.status = GetPrinterStatus((ipp_pstate_t)ippGetInteger(attr, 0));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT);
|
|
61
|
+
if (attr != NULL)
|
|
62
|
+
info.details["location"] = ippGetString(attr, 0, NULL);
|
|
63
|
+
|
|
64
|
+
attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT);
|
|
65
|
+
if (attr != NULL)
|
|
66
|
+
info.details["comment"] = ippGetString(attr, 0, NULL);
|
|
67
|
+
|
|
68
|
+
attr = ippFindAttribute(response, "printer-make-and-model", IPP_TAG_TEXT);
|
|
69
|
+
if (attr != NULL)
|
|
70
|
+
info.details["driver"] = ippGetString(attr, 0, NULL);
|
|
71
|
+
|
|
72
|
+
attr = ippFindAttribute(response, "port", IPP_TAG_TEXT);
|
|
73
|
+
if (attr != NULL)
|
|
74
|
+
info.details["port"] = ippGetString(attr, 0, NULL);
|
|
75
|
+
|
|
76
|
+
ippDelete(response);
|
|
77
|
+
}
|
|
78
|
+
httpClose(http);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
cupsFreeDests(num_dests, dests);
|
|
82
|
+
return info;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
std::vector<PrinterInfo> LinuxPrinter::GetPrinters()
|
|
86
|
+
{
|
|
87
|
+
std::vector<PrinterInfo> printers;
|
|
88
|
+
cups_dest_t *dests;
|
|
89
|
+
int num_dests = cupsGetDests(&dests);
|
|
90
|
+
|
|
91
|
+
for (int i = 0; i < num_dests; i++)
|
|
92
|
+
{
|
|
93
|
+
printers.push_back(GetPrinterDetails(dests[i].name, dests[i].is_default));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
cupsFreeDests(num_dests, dests);
|
|
97
|
+
return printers;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
PrinterInfo LinuxPrinter::GetSystemDefaultPrinter()
|
|
101
|
+
{
|
|
102
|
+
cups_dest_t *dests;
|
|
103
|
+
int num_dests = cupsGetDests(&dests);
|
|
104
|
+
cups_dest_t *dest = cupsGetDest(NULL, NULL, num_dests, dests);
|
|
105
|
+
|
|
106
|
+
PrinterInfo printer;
|
|
107
|
+
if (dest != NULL)
|
|
108
|
+
{
|
|
109
|
+
printer = GetPrinterDetails(dest->name, true);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
cupsFreeDests(num_dests, dests);
|
|
113
|
+
return printer;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
bool LinuxPrinter::PrintDirect(const std::string &printerName,
|
|
117
|
+
const std::vector<uint8_t> &data,
|
|
118
|
+
const std::string &dataType)
|
|
119
|
+
{
|
|
120
|
+
int jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, printerName.c_str(),
|
|
121
|
+
"Node.js Print Job", 0, NULL);
|
|
122
|
+
|
|
123
|
+
if (jobId <= 0)
|
|
124
|
+
return false;
|
|
125
|
+
|
|
126
|
+
http_status_t status = cupsStartDocument(CUPS_HTTP_DEFAULT, printerName.c_str(),
|
|
127
|
+
jobId, "Node.js Print Job",
|
|
128
|
+
dataType.c_str(), 1);
|
|
129
|
+
|
|
130
|
+
if (status != HTTP_STATUS_CONTINUE)
|
|
131
|
+
{
|
|
132
|
+
cupsCancelJob(printerName.c_str(), jobId);
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (cupsWriteRequestData(CUPS_HTTP_DEFAULT,
|
|
137
|
+
reinterpret_cast<const char *>(data.data()),
|
|
138
|
+
data.size()) != HTTP_STATUS_CONTINUE)
|
|
139
|
+
{
|
|
140
|
+
cupsCancelJob(printerName.c_str(), jobId);
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
status = static_cast<http_status_t>(cupsFinishDocument(CUPS_HTTP_DEFAULT, printerName.c_str()));
|
|
145
|
+
return status == HTTP_STATUS_OK;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
PrinterInfo LinuxPrinter::GetStatusPrinter(const std::string &printerName)
|
|
149
|
+
{
|
|
150
|
+
cups_dest_t *dests;
|
|
151
|
+
int num_dests = cupsGetDests(&dests);
|
|
152
|
+
|
|
153
|
+
cups_dest_t *defaultDest = cupsGetDest(NULL, NULL, num_dests, dests);
|
|
154
|
+
bool isDefault = false;
|
|
155
|
+
|
|
156
|
+
if (defaultDest != NULL)
|
|
157
|
+
{
|
|
158
|
+
isDefault = (printerName == defaultDest->name);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
cups_dest_t *dest = cupsGetDest(printerName.c_str(), NULL, num_dests, dests);
|
|
162
|
+
PrinterInfo printer;
|
|
163
|
+
|
|
164
|
+
if (dest != NULL)
|
|
165
|
+
{
|
|
166
|
+
printer = GetPrinterDetails(printerName, isDefault);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
cupsFreeDests(num_dests, dests);
|
|
170
|
+
return printer;
|
|
171
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#ifndef LINUX_PRINTER_H
|
|
2
|
+
#define LINUX_PRINTER_H
|
|
3
|
+
#include <cups/cups.h>
|
|
4
|
+
#include <cups/ppd.h>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include "printer_interface.h"
|
|
7
|
+
|
|
8
|
+
class LinuxPrinter : public PrinterInterface
|
|
9
|
+
{
|
|
10
|
+
private:
|
|
11
|
+
std::string GetPrinterStatus(ipp_pstate_t state);
|
|
12
|
+
|
|
13
|
+
public:
|
|
14
|
+
virtual PrinterInfo GetPrinterDetails(const std::string &printerName, bool isDefault = false) override;
|
|
15
|
+
virtual std::vector<PrinterInfo> GetPrinters() override;
|
|
16
|
+
virtual PrinterInfo GetSystemDefaultPrinter() override;
|
|
17
|
+
virtual bool PrintDirect(const std::string &printerName, const std::vector<uint8_t> &data, const std::string &dataType) override;
|
|
18
|
+
virtual PrinterInfo GetStatusPrinter(const std::string &printerName) override;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
#endif
|