@esslassi/electron-printer 0.0.5 → 0.0.7

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/test.js ADDED
@@ -0,0 +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
+
120
+ run().catch(console.error);