@esslassi/electron-printer 0.0.7 → 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 +318 -318
- package/binding.gyp +57 -57
- package/lib/electronPrinter.js +2 -2
- package/package.json +1 -1
- package/src/linux_printer.cpp +370 -370
- package/src/linux_printer.h +30 -30
- package/src/mac_printer.cpp +372 -372
- package/src/mac_printer.h +30 -30
- package/src/main.cpp +50 -50
- package/src/print.cpp +352 -352
- package/src/printer_factory.cpp +19 -19
- package/src/printer_factory.h +12 -12
- package/src/printer_interface.h +66 -66
- package/src/windows_printer.cpp +346 -346
- package/src/windows_printer.h +63 -63
- package/test.js +119 -119
package/src/windows_printer.h
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
#ifndef WINDOWS_PRINTER_H
|
|
2
|
-
#define WINDOWS_PRINTER_H
|
|
3
|
-
|
|
4
|
-
#include "printer_interface.h"
|
|
5
|
-
|
|
6
|
-
#include <windows.h>
|
|
7
|
-
#include <winspool.h>
|
|
8
|
-
|
|
9
|
-
/* Now remove macro pollution */
|
|
10
|
-
#ifdef GetDefaultPrinter
|
|
11
|
-
#undef GetDefaultPrinter
|
|
12
|
-
#endif
|
|
13
|
-
|
|
14
|
-
#ifdef GetDefaultPrinter
|
|
15
|
-
#undef GetDefaultPrinter
|
|
16
|
-
#endif
|
|
17
|
-
|
|
18
|
-
#ifdef GetPrinter
|
|
19
|
-
#undef GetPrinter
|
|
20
|
-
#endif
|
|
21
|
-
|
|
22
|
-
#ifdef GetJob
|
|
23
|
-
#undef GetJob
|
|
24
|
-
#endif
|
|
25
|
-
|
|
26
|
-
#ifdef SetJob
|
|
27
|
-
#undef SetJob
|
|
28
|
-
#endif
|
|
29
|
-
|
|
30
|
-
#include <vector>
|
|
31
|
-
#include <string>
|
|
32
|
-
#include <cstdint>
|
|
33
|
-
|
|
34
|
-
class WindowsPrinter : public PrinterInterface
|
|
35
|
-
{
|
|
36
|
-
private:
|
|
37
|
-
std::wstring Utf8ToWide(const std::string &str);
|
|
38
|
-
std::string WideToUtf8(LPWSTR wstr);
|
|
39
|
-
std::vector<std::string> MapJobStatus(DWORD status);
|
|
40
|
-
|
|
41
|
-
public:
|
|
42
|
-
std::vector<PrinterDetailsNative> GetPrinters() override;
|
|
43
|
-
PrinterDetailsNative GetPrinter(const std::string &printerName) override;
|
|
44
|
-
std::string GetDefaultPrinterName() override;
|
|
45
|
-
|
|
46
|
-
DriverOptions GetPrinterDriverOptions(const std::string &printerName) override;
|
|
47
|
-
std::string GetSelectedPaperSize(const std::string &printerName) override;
|
|
48
|
-
|
|
49
|
-
int PrintDirect(const std::string &printerName,
|
|
50
|
-
const std::vector<uint8_t> &data,
|
|
51
|
-
const std::string &type,
|
|
52
|
-
const StringMap &options) override;
|
|
53
|
-
|
|
54
|
-
int PrintFile(const std::string &printerName,
|
|
55
|
-
const std::string &filename) override;
|
|
56
|
-
|
|
57
|
-
std::vector<std::string> GetSupportedPrintFormats() override;
|
|
58
|
-
|
|
59
|
-
JobDetailsNative GetJob(const std::string &printerName, int jobId) override;
|
|
60
|
-
void SetJob(const std::string &printerName, int jobId, const std::string &command) override;
|
|
61
|
-
std::vector<std::string> GetSupportedJobCommands() override;
|
|
62
|
-
};
|
|
63
|
-
|
|
1
|
+
#ifndef WINDOWS_PRINTER_H
|
|
2
|
+
#define WINDOWS_PRINTER_H
|
|
3
|
+
|
|
4
|
+
#include "printer_interface.h"
|
|
5
|
+
|
|
6
|
+
#include <windows.h>
|
|
7
|
+
#include <winspool.h>
|
|
8
|
+
|
|
9
|
+
/* Now remove macro pollution */
|
|
10
|
+
#ifdef GetDefaultPrinter
|
|
11
|
+
#undef GetDefaultPrinter
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
#ifdef GetDefaultPrinter
|
|
15
|
+
#undef GetDefaultPrinter
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
#ifdef GetPrinter
|
|
19
|
+
#undef GetPrinter
|
|
20
|
+
#endif
|
|
21
|
+
|
|
22
|
+
#ifdef GetJob
|
|
23
|
+
#undef GetJob
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
#ifdef SetJob
|
|
27
|
+
#undef SetJob
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
#include <vector>
|
|
31
|
+
#include <string>
|
|
32
|
+
#include <cstdint>
|
|
33
|
+
|
|
34
|
+
class WindowsPrinter : public PrinterInterface
|
|
35
|
+
{
|
|
36
|
+
private:
|
|
37
|
+
std::wstring Utf8ToWide(const std::string &str);
|
|
38
|
+
std::string WideToUtf8(LPWSTR wstr);
|
|
39
|
+
std::vector<std::string> MapJobStatus(DWORD status);
|
|
40
|
+
|
|
41
|
+
public:
|
|
42
|
+
std::vector<PrinterDetailsNative> GetPrinters() override;
|
|
43
|
+
PrinterDetailsNative GetPrinter(const std::string &printerName) override;
|
|
44
|
+
std::string GetDefaultPrinterName() override;
|
|
45
|
+
|
|
46
|
+
DriverOptions GetPrinterDriverOptions(const std::string &printerName) override;
|
|
47
|
+
std::string GetSelectedPaperSize(const std::string &printerName) override;
|
|
48
|
+
|
|
49
|
+
int PrintDirect(const std::string &printerName,
|
|
50
|
+
const std::vector<uint8_t> &data,
|
|
51
|
+
const std::string &type,
|
|
52
|
+
const StringMap &options) override;
|
|
53
|
+
|
|
54
|
+
int PrintFile(const std::string &printerName,
|
|
55
|
+
const std::string &filename) override;
|
|
56
|
+
|
|
57
|
+
std::vector<std::string> GetSupportedPrintFormats() override;
|
|
58
|
+
|
|
59
|
+
JobDetailsNative GetJob(const std::string &printerName, int jobId) override;
|
|
60
|
+
void SetJob(const std::string &printerName, int jobId, const std::string &command) override;
|
|
61
|
+
std::vector<std::string> GetSupportedJobCommands() override;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
64
|
#endif
|
package/test.js
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
const printer = require('./lib');
|
|
2
|
-
|
|
3
|
-
async function run() {
|
|
4
|
-
console.log("===== TEST START =====");
|
|
5
|
-
|
|
6
|
-
// -------------------------------------------------
|
|
7
|
-
// 1️⃣ Get printers (sync)
|
|
8
|
-
// -------------------------------------------------
|
|
9
|
-
const printers = printer.getPrinters();
|
|
10
|
-
console.log("Printers:", printers);
|
|
11
|
-
|
|
12
|
-
if (!printers.length) {
|
|
13
|
-
console.log("No printers found. Exiting.");
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const defaultPrinter = printer.getDefaultPrinterName();
|
|
18
|
-
console.log("Default printer:", defaultPrinter);
|
|
19
|
-
|
|
20
|
-
const selectedPrinter = defaultPrinter || printers[0].name;
|
|
21
|
-
console.log("Using printer:", selectedPrinter);
|
|
22
|
-
|
|
23
|
-
// -------------------------------------------------
|
|
24
|
-
// 2️⃣ Get single printer
|
|
25
|
-
// -------------------------------------------------
|
|
26
|
-
const printerDetails = printer.getPrinter(selectedPrinter);
|
|
27
|
-
console.log("Printer details:", printerDetails);
|
|
28
|
-
|
|
29
|
-
// -------------------------------------------------
|
|
30
|
-
// 3️⃣ Driver options
|
|
31
|
-
// -------------------------------------------------
|
|
32
|
-
const driverOptions = printer.getPrinterDriverOptions(selectedPrinter);
|
|
33
|
-
console.log("Driver options:", driverOptions);
|
|
34
|
-
|
|
35
|
-
// -------------------------------------------------
|
|
36
|
-
// 4️⃣ Paper size
|
|
37
|
-
// -------------------------------------------------
|
|
38
|
-
const paper = printer.getSelectedPaperSize(selectedPrinter);
|
|
39
|
-
console.log("Selected paper size:", paper);
|
|
40
|
-
|
|
41
|
-
// -------------------------------------------------
|
|
42
|
-
// 5️⃣ Supported formats
|
|
43
|
-
// -------------------------------------------------
|
|
44
|
-
console.log("Supported formats:", printer.getSupportedPrintFormats());
|
|
45
|
-
|
|
46
|
-
// -------------------------------------------------
|
|
47
|
-
// 6️⃣ Supported job commands
|
|
48
|
-
// -------------------------------------------------
|
|
49
|
-
console.log("Supported job commands:", printer.getSupportedJobCommands());
|
|
50
|
-
|
|
51
|
-
// -------------------------------------------------
|
|
52
|
-
// 7️⃣ printDirect (callback version)
|
|
53
|
-
// -------------------------------------------------
|
|
54
|
-
printer.printDirect({
|
|
55
|
-
data: "TEST PRINT FROM CALLBACK\n\n",
|
|
56
|
-
printer: selectedPrinter,
|
|
57
|
-
type: "RAW",
|
|
58
|
-
success: (jobId) => {
|
|
59
|
-
console.log("printDirect success jobId:", jobId);
|
|
60
|
-
|
|
61
|
-
const job = printer.getJob(selectedPrinter, parseInt(jobId));
|
|
62
|
-
console.log("Job details (sync):", job);
|
|
63
|
-
|
|
64
|
-
// Try cancel test (optional)
|
|
65
|
-
// printer.setJob(selectedPrinter, parseInt(jobId), "CANCEL");
|
|
66
|
-
},
|
|
67
|
-
error: (err) => {
|
|
68
|
-
console.error("printDirect error:", err);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
// -------------------------------------------------
|
|
73
|
-
// 8️⃣ printDirectAsync
|
|
74
|
-
// -------------------------------------------------
|
|
75
|
-
try {
|
|
76
|
-
const jobId = await printer.printDirectAsync({
|
|
77
|
-
data: Buffer.from("TEST PRINT FROM ASYNC\n\n"),
|
|
78
|
-
printer: selectedPrinter,
|
|
79
|
-
type: "RAW"
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
console.log("printDirectAsync jobId:", jobId);
|
|
83
|
-
|
|
84
|
-
const jobAsync = await printer.getJobAsync(selectedPrinter, parseInt(jobId));
|
|
85
|
-
console.log("Job details (async):", jobAsync);
|
|
86
|
-
|
|
87
|
-
} catch (err) {
|
|
88
|
-
console.error("printDirectAsync error:", err);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// -------------------------------------------------
|
|
92
|
-
// 9️⃣ printFileAsync
|
|
93
|
-
// -------------------------------------------------
|
|
94
|
-
try {
|
|
95
|
-
const jobId = await printer.printFileAsync({
|
|
96
|
-
filename: "./sample.txt", // make sure this exists
|
|
97
|
-
printer: selectedPrinter
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
console.log("printFileAsync jobId:", jobId);
|
|
101
|
-
} catch (err) {
|
|
102
|
-
console.error("printFileAsync error:", err);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// -------------------------------------------------
|
|
106
|
-
// 🔟 getPrintersAsync
|
|
107
|
-
// -------------------------------------------------
|
|
108
|
-
const printersAsync = await printer.getPrintersAsync();
|
|
109
|
-
console.log("Printers (async):", printersAsync);
|
|
110
|
-
|
|
111
|
-
// -------------------------------------------------
|
|
112
|
-
// 11️⃣ getPrinterAsync
|
|
113
|
-
// -------------------------------------------------
|
|
114
|
-
const singlePrinterAsync = await printer.getPrinterAsync(selectedPrinter);
|
|
115
|
-
console.log("Single printer (async):", singlePrinterAsync);
|
|
116
|
-
|
|
117
|
-
console.log("===== TEST END =====");
|
|
118
|
-
}
|
|
119
|
-
|
|
1
|
+
const printer = require('./lib');
|
|
2
|
+
|
|
3
|
+
async function run() {
|
|
4
|
+
console.log("===== TEST START =====");
|
|
5
|
+
|
|
6
|
+
// -------------------------------------------------
|
|
7
|
+
// 1️⃣ Get printers (sync)
|
|
8
|
+
// -------------------------------------------------
|
|
9
|
+
const printers = printer.getPrinters();
|
|
10
|
+
console.log("Printers:", printers);
|
|
11
|
+
|
|
12
|
+
if (!printers.length) {
|
|
13
|
+
console.log("No printers found. Exiting.");
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const defaultPrinter = printer.getDefaultPrinterName();
|
|
18
|
+
console.log("Default printer:", defaultPrinter);
|
|
19
|
+
|
|
20
|
+
const selectedPrinter = defaultPrinter || printers[0].name;
|
|
21
|
+
console.log("Using printer:", selectedPrinter);
|
|
22
|
+
|
|
23
|
+
// -------------------------------------------------
|
|
24
|
+
// 2️⃣ Get single printer
|
|
25
|
+
// -------------------------------------------------
|
|
26
|
+
const printerDetails = printer.getPrinter(selectedPrinter);
|
|
27
|
+
console.log("Printer details:", printerDetails);
|
|
28
|
+
|
|
29
|
+
// -------------------------------------------------
|
|
30
|
+
// 3️⃣ Driver options
|
|
31
|
+
// -------------------------------------------------
|
|
32
|
+
const driverOptions = printer.getPrinterDriverOptions(selectedPrinter);
|
|
33
|
+
console.log("Driver options:", driverOptions);
|
|
34
|
+
|
|
35
|
+
// -------------------------------------------------
|
|
36
|
+
// 4️⃣ Paper size
|
|
37
|
+
// -------------------------------------------------
|
|
38
|
+
const paper = printer.getSelectedPaperSize(selectedPrinter);
|
|
39
|
+
console.log("Selected paper size:", paper);
|
|
40
|
+
|
|
41
|
+
// -------------------------------------------------
|
|
42
|
+
// 5️⃣ Supported formats
|
|
43
|
+
// -------------------------------------------------
|
|
44
|
+
console.log("Supported formats:", printer.getSupportedPrintFormats());
|
|
45
|
+
|
|
46
|
+
// -------------------------------------------------
|
|
47
|
+
// 6️⃣ Supported job commands
|
|
48
|
+
// -------------------------------------------------
|
|
49
|
+
console.log("Supported job commands:", printer.getSupportedJobCommands());
|
|
50
|
+
|
|
51
|
+
// -------------------------------------------------
|
|
52
|
+
// 7️⃣ printDirect (callback version)
|
|
53
|
+
// -------------------------------------------------
|
|
54
|
+
printer.printDirect({
|
|
55
|
+
data: "TEST PRINT FROM CALLBACK\n\n",
|
|
56
|
+
printer: selectedPrinter,
|
|
57
|
+
type: "RAW",
|
|
58
|
+
success: (jobId) => {
|
|
59
|
+
console.log("printDirect success jobId:", jobId);
|
|
60
|
+
|
|
61
|
+
const job = printer.getJob(selectedPrinter, parseInt(jobId));
|
|
62
|
+
console.log("Job details (sync):", job);
|
|
63
|
+
|
|
64
|
+
// Try cancel test (optional)
|
|
65
|
+
// printer.setJob(selectedPrinter, parseInt(jobId), "CANCEL");
|
|
66
|
+
},
|
|
67
|
+
error: (err) => {
|
|
68
|
+
console.error("printDirect error:", err);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// -------------------------------------------------
|
|
73
|
+
// 8️⃣ printDirectAsync
|
|
74
|
+
// -------------------------------------------------
|
|
75
|
+
try {
|
|
76
|
+
const jobId = await printer.printDirectAsync({
|
|
77
|
+
data: Buffer.from("TEST PRINT FROM ASYNC\n\n"),
|
|
78
|
+
printer: selectedPrinter,
|
|
79
|
+
type: "RAW"
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
console.log("printDirectAsync jobId:", jobId);
|
|
83
|
+
|
|
84
|
+
const jobAsync = await printer.getJobAsync(selectedPrinter, parseInt(jobId));
|
|
85
|
+
console.log("Job details (async):", jobAsync);
|
|
86
|
+
|
|
87
|
+
} catch (err) {
|
|
88
|
+
console.error("printDirectAsync error:", err);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// -------------------------------------------------
|
|
92
|
+
// 9️⃣ printFileAsync
|
|
93
|
+
// -------------------------------------------------
|
|
94
|
+
try {
|
|
95
|
+
const jobId = await printer.printFileAsync({
|
|
96
|
+
filename: "./sample.txt", // make sure this exists
|
|
97
|
+
printer: selectedPrinter
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
console.log("printFileAsync jobId:", jobId);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.error("printFileAsync error:", err);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// -------------------------------------------------
|
|
106
|
+
// 🔟 getPrintersAsync
|
|
107
|
+
// -------------------------------------------------
|
|
108
|
+
const printersAsync = await printer.getPrintersAsync();
|
|
109
|
+
console.log("Printers (async):", printersAsync);
|
|
110
|
+
|
|
111
|
+
// -------------------------------------------------
|
|
112
|
+
// 11️⃣ getPrinterAsync
|
|
113
|
+
// -------------------------------------------------
|
|
114
|
+
const singlePrinterAsync = await printer.getPrinterAsync(selectedPrinter);
|
|
115
|
+
console.log("Single printer (async):", singlePrinterAsync);
|
|
116
|
+
|
|
117
|
+
console.log("===== TEST END =====");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
120
|
run().catch(console.error);
|