@esslassi/electron-printer 0.0.6 → 0.0.8

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 CHANGED
@@ -1,251 +1,319 @@
1
- Here is the clean **README.md** content ready to copy and paste:
1
+ # 📦 @esslassi/electron-printer
2
2
 
3
- ---
3
+ Cross-platform native printer driver for **Windows, macOS, and Linux**, built with Node-API (N-API).
4
+ Works in **Node.js** and **Electron**.
4
5
 
5
- # Electron Printer (@esslassi/electron-printer)
6
+ Supports:
6
7
 
7
- Node.js and Electron bindings for printer management and direct printing. Supports Windows and Linux (CUPS).
8
+ * 🖨 List printers
9
+ * 📄 Print raw/text/command data
10
+ * 📁 Print files
11
+ * 📋 Get printer driver options
12
+ * 📑 Get selected paper size
13
+ * 📦 Get job status
14
+ * ⛔ Cancel / pause / resume jobs
15
+ * 🔄 Promise (async/await) wrappers included
8
16
 
9
- ## Features
17
+ ---
10
18
 
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
19
+ ## 🚀 Installation
18
20
 
19
- ## Requirements
21
+ ```bash
22
+ npm install @esslassi/electron-printer
23
+ ```
20
24
 
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
25
+ If using Electron:
26
26
 
27
27
  ```bash
28
- sudo apt-get install libcups2-dev
28
+ npx electron-rebuild
29
29
  ```
30
30
 
31
- ## Installation
31
+ ---
32
+
33
+ ## 🧩 Supported Platforms
32
34
 
33
- ```bash
34
- npm install @esslassi/electron-printer
35
+ | OS | Backend |
36
+ | ------- | ------------ |
37
+ | Windows | WinSpool API |
38
+ | macOS | CUPS |
39
+ | Linux | CUPS |
40
+
41
+ ---
42
+
43
+ # 📖 Usage
44
+
45
+ ```ts
46
+ import * as printer from '@esslassi/electron-printer'
35
47
  ```
36
48
 
37
- For development:
49
+ ---
38
50
 
39
- ```bash
40
- git clone https://github.com/esslassi/electron-printer.git
41
- cd electron-printer
42
- npm install
43
- npm run rebuild
51
+ # 🖨 Printer Management
52
+
53
+ ### Get all printers
54
+
55
+ ```ts
56
+ const printers = printer.getPrinters()
44
57
  ```
45
58
 
46
- ## Usage
59
+ ### Async version
47
60
 
48
- ### JavaScript
61
+ ```ts
62
+ const printers = await printer.getPrintersAsync()
63
+ ```
49
64
 
50
- ```javascript
51
- const printer = require('@esslassi/electron-printer');
65
+ ---
52
66
 
53
- // List all printers
54
- printer.getPrinters()
55
- .then(printers => {
56
- console.log('Available printers:', printers);
57
- })
58
- .catch(console.error);
67
+ ### Get single printer
59
68
 
60
- // Get default printer
61
- printer.getDefaultPrinter()
62
- .then(defaultPrinter => {
63
- console.log('Default printer:', defaultPrinter);
64
- })
65
- .catch(console.error);
69
+ ```ts
70
+ const details = printer.getPrinter("My Printer")
71
+ ```
66
72
 
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);
73
+ ---
74
+
75
+ ### Get default printer
76
+
77
+ ```ts
78
+ const defaultPrinter = printer.getDefaultPrinterName()
86
79
  ```
87
80
 
88
- ### TypeScript
81
+ ---
82
+
83
+ ### Get printer driver options
89
84
 
90
- ```typescript
91
- import printer, {
92
- Printer,
93
- PrintDirectOptions,
94
- GetStatusPrinterOptions
95
- } from '@esslassi/electron-printer';
85
+ ```ts
86
+ const options = printer.getPrinterDriverOptions("My Printer")
87
+ ```
88
+
89
+ ---
96
90
 
97
- async function example() {
98
- try {
99
- const printers: Printer[] = await printer.getPrinters();
100
- console.log('Printers:', printers);
91
+ ### Get selected paper size
101
92
 
102
- const defaultPrinter: Printer = await printer.getDefaultPrinter();
103
- console.log('Default printer:', defaultPrinter);
93
+ ```ts
94
+ const paper = printer.getSelectedPaperSize("My Printer")
95
+ ```
104
96
 
105
- const statusOptions: GetStatusPrinterOptions = {
106
- printerName: 'Printer Name'
107
- };
108
- const status: Printer = await printer.getStatusPrinter(statusOptions);
109
- console.log('Status:', status);
97
+ ---
110
98
 
111
- const printOptions: PrintDirectOptions = {
112
- printerName: 'Printer Name',
113
- data: Buffer.from('Text to print'),
114
- dataType: 'RAW'
115
- };
99
+ # 🖨 Printing
116
100
 
117
- const result = await printer.printDirect(printOptions);
118
- console.log('Result:', result);
101
+ ---
119
102
 
120
- } catch (error) {
121
- console.error('Error:', error);
103
+ ## 🟢 Print Raw/Text Data (Callback)
104
+
105
+ ```ts
106
+ printer.printDirect({
107
+ data: "Hello Printer\n\n",
108
+ printer: "My Printer",
109
+ type: "RAW",
110
+ success: (jobId) => {
111
+ console.log("Printed. Job ID:", jobId)
112
+ },
113
+ error: (err) => {
114
+ console.error("Print error:", err)
122
115
  }
123
- }
116
+ })
124
117
  ```
125
118
 
126
- ## API
119
+ ---
127
120
 
128
- ### getPrinters(): Promise<Printer[]>
121
+ ## 🟢 Print Raw/Text Data (Async/Await)
129
122
 
130
- Lists all printers installed on the system.
123
+ ```ts
124
+ const jobId = await printer.printDirectAsync({
125
+ data: Buffer.from("Hello Printer\n\n"),
126
+ printer: "My Printer",
127
+ type: "RAW"
128
+ })
131
129
 
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
- }
130
+ console.log("Printed. Job ID:", jobId)
145
131
  ```
146
132
 
147
133
  ---
148
134
 
149
- ### getDefaultPrinter(): Promise<Printer>
135
+ ## 🟢 Print File (Callback)
150
136
 
151
- Returns the system default printer.
137
+ ```ts
138
+ printer.printFile({
139
+ filename: "./file.txt",
140
+ printer: "My Printer",
141
+ success: (jobId) => console.log(jobId),
142
+ error: (err) => console.error(err)
143
+ })
144
+ ```
152
145
 
153
146
  ---
154
147
 
155
- ### getStatusPrinter(options: GetStatusPrinterOptions): Promise<Printer>
148
+ ## 🟢 Print File (Async)
156
149
 
157
- ```typescript
158
- interface GetStatusPrinterOptions {
159
- printerName: string;
160
- }
150
+ ```ts
151
+ const jobId = await printer.printFileAsync({
152
+ filename: "./file.txt",
153
+ printer: "My Printer"
154
+ })
161
155
  ```
162
156
 
163
157
  ---
164
158
 
165
- ### printDirect(options: PrintDirectOptions): Promise<string>
159
+ # 📦 Job Management
166
160
 
167
- ```typescript
168
- interface PrintDirectOptions {
169
- printerName: string;
170
- data: string | Buffer;
171
- dataType?: 'RAW' | 'TEXT' | 'COMMAND' | 'AUTO';
172
- }
161
+ ---
162
+
163
+ ## Get Job Details
164
+
165
+ ```ts
166
+ const job = printer.getJob("My Printer", 42)
167
+ ```
168
+
169
+ ### Async
170
+
171
+ ```ts
172
+ const job = await printer.getJobAsync("My Printer", 42)
173
173
  ```
174
174
 
175
- ### Possible Status Values
175
+ ---
176
+
177
+ ## Cancel / Pause / Resume Job
176
178
 
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
179
+ ```ts
180
+ printer.setJob("My Printer", 42, "CANCEL")
181
+ printer.setJob("My Printer", 42, "PAUSE")
182
+ printer.setJob("My Printer", 42, "RESUME")
183
+ ```
198
184
 
199
185
  ---
200
186
 
201
- ## Supported Platforms
187
+ ## Get Supported Job Commands
202
188
 
203
- * Windows (32/64-bit)
204
- * Linux (CUPS)
189
+ ```ts
190
+ const commands = printer.getSupportedJobCommands()
191
+ ```
205
192
 
206
193
  ---
207
194
 
208
- ## Troubleshooting
195
+ # 📄 Supported Print Formats
196
+
197
+ ```ts
198
+ const formats = printer.getSupportedPrintFormats()
199
+ ```
209
200
 
210
- ### Windows
201
+ Typical values:
211
202
 
212
- 1. Install Visual Studio Build Tools
213
- 2. Run:
203
+ * `RAW`
204
+ * `TEXT`
205
+ * `COMMAND`
206
+ * `AUTO`
214
207
 
215
- ```bash
216
- npm run rebuild
208
+ > Windows supports RAW/TEXT/COMMAND directly.
209
+ > macOS/Linux support additional formats via CUPS.
210
+
211
+ ---
212
+
213
+ # 🧠 TypeScript Types
214
+
215
+ Fully typed. Example:
216
+
217
+ ```ts
218
+ export interface PrintDirectOptions {
219
+ data: string | Buffer
220
+ printer?: string
221
+ type?: 'RAW' | 'TEXT' | 'COMMAND' | 'AUTO'
222
+ options?: { [key: string]: string }
223
+ }
217
224
  ```
218
225
 
219
- 3. Verify printer access permissions
226
+ ---
227
+
228
+ # ⚡ Promise Wrappers Included
220
229
 
221
- ### Linux
230
+ Built-in async wrappers:
222
231
 
223
- ```bash
224
- sudo apt-get install libcups2-dev
225
- sudo service cups status
226
- sudo usermod -a -G lp $USER
232
+ * `printDirectAsync`
233
+ * `printFileAsync`
234
+ * `getPrintersAsync`
235
+ * `getPrinterAsync`
236
+ * `getJobAsync`
237
+
238
+ No extra wrapper needed.
239
+
240
+ ---
241
+
242
+ # 🧪 Example Full Test
243
+
244
+ ```ts
245
+ import * as printer from '@esslassi/electron-printer'
246
+
247
+ async function test() {
248
+ const printers = await printer.getPrintersAsync()
249
+ const defaultPrinter = printer.getDefaultPrinterName()
250
+
251
+ const jobId = await printer.printDirectAsync({
252
+ data: "Test print\n\n",
253
+ printer: defaultPrinter,
254
+ type: "RAW"
255
+ })
256
+
257
+ const job = await printer.getJobAsync(defaultPrinter!, Number(jobId))
258
+
259
+ console.log(job)
260
+ }
261
+
262
+ test()
227
263
  ```
228
264
 
229
265
  ---
230
266
 
231
- ## Development
267
+ # 🏗 Architecture
268
+
269
+ ```
270
+ Node.js / Electron
271
+
272
+ N-API
273
+
274
+ PrinterFactory
275
+
276
+ Windows | macOS | Linux drivers
277
+ ```
278
+
279
+ Native C++ backend with platform-specific implementations.
280
+
281
+ ---
282
+
283
+ # 🔧 Development
284
+
285
+ Build from source:
286
+
287
+ ```bash
288
+ npx node-gyp rebuild
289
+ ```
290
+
291
+ Electron:
232
292
 
233
293
  ```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
294
+ npx electron-rebuild
239
295
  ```
240
296
 
241
297
  ---
242
298
 
243
- ## License
299
+ # 📜 License
244
300
 
245
301
  MIT
246
302
 
247
303
  ---
248
304
 
249
- ## Author
305
+ # 👨‍💻 Author
306
+
307
+ Esslassi
308
+
309
+ ---
310
+
311
+ If you want, I can also generate:
312
+
313
+ * 🔥 NPM package.json template
314
+ * 🧱 binding.gyp optimized version
315
+ * ⚡ Prebuild support (no node-gyp required for users)
316
+ * 🧪 Jest test suite
317
+ * 📦 CLI tool version
250
318
 
251
- Esslassi
319
+ Tell me the next step 🚀
@@ -1,33 +1,61 @@
1
- export interface PrintOptions {
2
- printerName: string;
1
+ export type PrintOnSuccessFunction = (jobId: string) => any;
2
+ export type PrintOnErrorFunction = (err: Error) => any;
3
+ export interface PrintDirectOptions {
3
4
  data: string | Buffer;
4
- dataType?: 'RAW' | 'TEXT' | 'COMMAND' | 'AUTO' | undefined;
5
+ printer?: string;
6
+ type?: 'RAW' | 'TEXT' | 'COMMAND' | 'AUTO';
7
+ options?: {
8
+ [key: string]: string;
9
+ };
10
+ success?: PrintOnSuccessFunction;
11
+ error?: PrintOnErrorFunction;
5
12
  }
6
- export interface Printer {
13
+ export interface PrintFileOptions {
14
+ filename: string;
15
+ printer?: string;
16
+ success?: PrintOnSuccessFunction;
17
+ error?: PrintOnErrorFunction;
18
+ }
19
+ export interface PrinterDetails {
7
20
  name: string;
8
21
  isDefault: boolean;
9
- status: string;
10
- details: {
11
- location?: string;
12
- comment?: string;
13
- driver?: string;
14
- port?: string;
15
- [key: string]: string | undefined;
22
+ options: {
23
+ [key: string]: string;
16
24
  };
17
25
  }
18
- export interface PrintDirectOptions {
19
- printerName: string;
20
- data: string | Buffer;
21
- dataType?: 'RAW' | 'TEXT' | 'COMMAND' | 'AUTO' | undefined;
22
- }
23
- export interface GetStatusPrinterOptions {
24
- printerName: string;
26
+ export interface PrinterDriverOptions {
27
+ [key: string]: {
28
+ [key: string]: boolean;
29
+ };
25
30
  }
26
- export interface PrintDirectOutput {
31
+ export type JobStatus = 'PAUSED' | 'PRINTING' | 'PRINTED' | 'CANCELLED' | 'PENDING' | 'ABORTED';
32
+ export type JobCommand = "CANCEL" | "PAUSE" | "RESUME";
33
+ export interface JobDetails {
34
+ id: number;
27
35
  name: string;
28
- status: 'success' | 'failed';
36
+ printerName: string;
37
+ user: string;
38
+ format: string;
39
+ priority: number;
40
+ size: number;
41
+ status: JobStatus[];
42
+ completedTime: Date;
43
+ creationTime: Date;
44
+ processingTime: Date;
29
45
  }
30
- export declare function printDirect(printOptions: PrintOptions): Promise<PrintDirectOutput>;
31
- export declare function getStatusPrinter(printOptions: GetStatusPrinterOptions): Promise<Printer>;
32
- export declare function getPrinters(): Promise<Printer[]>;
33
- export declare function getDefaultPrinter(): Promise<Printer>;
46
+ export declare function getPrinters(): PrinterDetails[];
47
+ export declare function getPrinter(printerName: string): PrinterDetails;
48
+ export declare function getPrinterDriverOptions(printerName: string): PrinterDriverOptions;
49
+ export declare function getSelectedPaperSize(printerName: string): string;
50
+ export declare function getDefaultPrinterName(): string | undefined;
51
+ export declare function printDirect(options: PrintDirectOptions): void;
52
+ export declare function printFile(options: PrintFileOptions): void;
53
+ export declare function getSupportedPrintFormats(): string[];
54
+ export declare function getJob(printerName: string, jobId: number): JobDetails;
55
+ export declare function setJob(printerName: string, jobId: number, command: JobCommand): void;
56
+ export declare function getSupportedJobCommands(): string[];
57
+ export declare function printDirectAsync(options: Omit<PrintDirectOptions, 'success' | 'error'>): Promise<string>;
58
+ export declare function printFileAsync(options: Omit<PrintFileOptions, 'success' | 'error'>): Promise<string>;
59
+ export declare function getJobAsync(printerName: string, jobId: number): Promise<JobDetails>;
60
+ export declare function getPrintersAsync(): Promise<PrinterDetails[]>;
61
+ export declare function getPrinterAsync(printerName: string): Promise<PrinterDetails>;
@@ -3,36 +3,87 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.printDirect = printDirect;
7
- exports.getStatusPrinter = getStatusPrinter;
8
6
  exports.getPrinters = getPrinters;
9
- exports.getDefaultPrinter = getDefaultPrinter;
10
- const bindings_1 = __importDefault(require("bindings"));
11
- const electronPrinter = (0, bindings_1.default)('electron_printer');
12
- async function printDirect(printOptions) {
13
- const input = {
14
- ...printOptions,
15
- printerName: normalizeString(printOptions.printerName)
16
- };
17
- const printer = await electronPrinter.printDirect(input);
18
- return printer;
19
- }
20
- async function getStatusPrinter(printOptions) {
21
- const input = {
22
- ...printOptions,
23
- printerName: normalizeString(printOptions.printerName)
24
- };
25
- const printer = await electronPrinter.getStatusPrinter(input);
26
- return printer;
27
- }
28
- async function getPrinters() {
29
- const printers = await electronPrinter.getPrinters();
30
- return printers;
31
- }
32
- async function getDefaultPrinter() {
33
- const printer = await electronPrinter.getDefaultPrinter();
34
- return printer;
35
- }
36
- function normalizeString(str) {
37
- return String.raw `${str}`;
7
+ exports.getPrinter = getPrinter;
8
+ exports.getPrinterDriverOptions = getPrinterDriverOptions;
9
+ exports.getSelectedPaperSize = getSelectedPaperSize;
10
+ exports.getDefaultPrinterName = getDefaultPrinterName;
11
+ exports.printDirect = printDirect;
12
+ exports.printFile = printFile;
13
+ exports.getSupportedPrintFormats = getSupportedPrintFormats;
14
+ exports.getJob = getJob;
15
+ exports.setJob = setJob;
16
+ exports.getSupportedJobCommands = getSupportedJobCommands;
17
+ exports.printDirectAsync = printDirectAsync;
18
+ exports.printFileAsync = printFileAsync;
19
+ exports.getJobAsync = getJobAsync;
20
+ exports.getPrintersAsync = getPrintersAsync;
21
+ exports.getPrinterAsync = getPrinterAsync;
22
+ const path_1 = __importDefault(require("path"));
23
+ const native = require(path_1.default.join(__dirname, '../build/Release/electron_printer.node'));
24
+ /* ===========================
25
+ DIRECT NATIVE EXPORTS
26
+ =========================== */
27
+ function getPrinters() {
28
+ return native.getPrinters();
29
+ }
30
+ function getPrinter(printerName) {
31
+ return native.getPrinter(printerName);
32
+ }
33
+ function getPrinterDriverOptions(printerName) {
34
+ return native.getPrinterDriverOptions(printerName);
35
+ }
36
+ function getSelectedPaperSize(printerName) {
37
+ return native.getSelectedPaperSize(printerName);
38
+ }
39
+ function getDefaultPrinterName() {
40
+ return native.getDefaultPrinterName();
41
+ }
42
+ function printDirect(options) {
43
+ native.printDirect(options);
44
+ }
45
+ function printFile(options) {
46
+ native.printFile(options);
47
+ }
48
+ function getSupportedPrintFormats() {
49
+ return native.getSupportedPrintFormats();
50
+ }
51
+ function getJob(printerName, jobId) {
52
+ return native.getJob(printerName, jobId);
53
+ }
54
+ function setJob(printerName, jobId, command) {
55
+ native.setJob(printerName, jobId, command);
56
+ }
57
+ function getSupportedJobCommands() {
58
+ return native.getSupportedJobCommands();
59
+ }
60
+ /* ==================================================
61
+ PROMISE WRAPPERS (Async/Await Friendly)
62
+ ================================================== */
63
+ function printDirectAsync(options) {
64
+ return new Promise((resolve, reject) => {
65
+ native.printDirect({
66
+ ...options,
67
+ success: (jobId) => resolve(jobId),
68
+ error: (err) => reject(err)
69
+ });
70
+ });
71
+ }
72
+ function printFileAsync(options) {
73
+ return new Promise((resolve, reject) => {
74
+ native.printFile({
75
+ ...options,
76
+ success: (jobId) => resolve(jobId),
77
+ error: (err) => reject(err)
78
+ });
79
+ });
80
+ }
81
+ function getJobAsync(printerName, jobId) {
82
+ return Promise.resolve(native.getJob(printerName, jobId));
83
+ }
84
+ function getPrintersAsync() {
85
+ return Promise.resolve(native.getPrinters());
86
+ }
87
+ function getPrinterAsync(printerName) {
88
+ return Promise.resolve(native.getPrinter(printerName));
38
89
  }