-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
213 lines (177 loc) · 5.97 KB
/
Copy pathmain.js
File metadata and controls
213 lines (177 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const { app, BrowserWindow, dialog, ipcMain, shell } = require('electron');
const { exec } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const settings = require('electron-settings');
app.commandLine.appendSwitch('no-sandbox');
var win = null;
function createWindow() {
win = new BrowserWindow({
width: 430,
height: 550,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
}
})
win.removeMenu();
//win.webContents.openDevTools();
win.loadFile('src/index.html');
}
function prinTesTicket(printerName) {
var esc = '\x1B'; //ESC byte in hex notation
var gs = '\x1D'; //GS byte in hex notation
var newLine = '\x0A'; //LF byte in hex notation
var cmds = esc + "@"; //Initializes the printer (ESC @)
// Header
cmds += esc + '!' + '\x38'; //Emphasized + Double-height + Double-width mode selected (ESC ! (8 + 16 + 32)) 56 dec => 38 hex
cmds += 'PRUEBA'; //text to print
cmds += newLine + newLine;
cmds += esc + '!' + '\x00'; //Character font A selected (ESC ! 0)
// Sample items
cmds += 'COOKIES 5.00';
cmds += newLine;
cmds += 'MILK 65 Fl oz 3.78';
cmds += newLine + newLine;
cmds += 'SUBTOTAL 8.78';
cmds += newLine;
cmds += 'TAX 5% 0.44';
cmds += newLine;
cmds += 'TOTAL 9.22';
cmds += newLine;
cmds += 'CASH TEND 10.00';
cmds += newLine;
cmds += 'CASH DUE 0.78';
cmds += newLine + newLine;
// Print barcode - Code128
cmds += esc + 'a' + '\x01'; // Center alignment
cmds += 'CODIGO DE BARRAS:';
cmds += newLine;
cmds += gs + 'h' + '\x50'; // Set barcode height to 80 dots
cmds += gs + 'w' + '\x02'; // Set barcode width to 2
cmds += gs + 'H' + '\x02'; // Print HRI (human readable) below barcode
cmds += gs + 'k' + '\x49' + '\x0D' + '1234567890123'; // CODE128 barcode with content
cmds += newLine + newLine;
// Print QR Code
cmds += 'CODIGO QR:';
cmds += newLine;
// QR Code: Model
cmds += gs + '(k' + '\x04\x00' + '1A2\x00'; // Function 165: Set model (49=1, 50=2)
// QR Code: Size
cmds += gs + '(k' + '\x03\x00' + '1C' + '\x08'; // Function 167: Set size to 8
// QR Code: Error correction level
cmds += gs + '(k' + '\x03\x00' + '1E0'; // Function 169: Error correction L
// QR Code: Store data
var qrData = 'https://megacity20.com';
var qrDataLength = qrData.length + 3;
var pL = String.fromCharCode(qrDataLength % 256);
var pH = String.fromCharCode(Math.floor(qrDataLength / 256));
cmds += gs + '(k' + pL + pH + '1P0' + qrData; // Function 180: Store data
// QR Code: Print
cmds += gs + '(k' + '\x03\x00' + '1Q0'; // Function 181: Print QR code
cmds += newLine + newLine;
// Reset alignment
cmds += esc + 'a' + '\x00'; // Left alignment
cmds += newLine + newLine;
// add cut command (ESC m - partial cut)
cmds += '\x1B\x6D' + newLine;
// add cash drawer open command (ESC p 0 55 121 - standard command)
cmds += '\x1B\x70\x30\x37\x79' + newLine;
const ticketPath = path.join(app.getPath('temp'), 'fsprinter_ticket');
fs.writeFile(ticketPath, cmds, function (err) {
if (err) {
return console.log(err);
}
prinTicket(printerName);
});
// volcamos en consola la ruta del archivo
console.log('Ticket file path:', ticketPath);
}
function prinTicket(printerName) {
const ticketPath = path.join(app.getPath('temp'), 'fsprinter_ticket');
let printCmd;
if (os.platform() == 'win32') {
// Get the resources path properly
let resourcesPath;
if (app.isPackaged) {
// When packaged, unpacked files are in app.asar.unpacked
resourcesPath = path.join(process.resourcesPath, 'app.asar.unpacked');
} else {
// In development, use the app path directly
resourcesPath = app.getAppPath();
}
const rawPrintPath = path.join(resourcesPath, 'RawPrint.exe');
printCmd = '"' + rawPrintPath + '" "' + printerName + '" "' + ticketPath + '"';
} else if (os.platform() == 'darwin') {
// macOS: usar lp con ruta absoluta
printCmd = 'lp -d "' + printerName + '" -o raw "' + ticketPath + '"';
} else {
// Linux
printCmd = 'lp -d "' + printerName + '" "' + ticketPath + '"';
}
console.log('Print command:', printCmd);
exec(printCmd, (error, stdout, stderr) => {
if(stdout) {
console.log(`stdout: ${stdout}`);
}
if (error) {
console.log(`error: ${error.message}`);
dialog.showErrorBox("print error", error.message);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
});
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
ipcMain.handle('get-printers', async () => {
return await win.webContents.getPrintersAsync();
})
ipcMain.on('open-external', (event, url) => {
shell.openExternal(url);
})
ipcMain.on('reload-window', () => {
if (win) win.reload();
})
ipcMain.handle('settings-get', async (event, key) => {
return await settings.get(key);
})
ipcMain.handle('settings-set', async (event, key, value) => {
return await settings.set(key, value);
})
ipcMain.handle('settings-has', async (event, key) => {
return await settings.has(key);
})
ipcMain.handle('settings-unset', async () => {
return await settings.unset();
})
ipcMain.handle('get-app-version', () => app.getVersion())
ipcMain.on('print-test', (event, arg) => {
prinTesTicket(arg);
event.reply('print-test', 'ok');
})
ipcMain.on('send-to-printer', (event, arg) => {
console.log('send-to-printer');
const ticketPath = path.join(app.getPath('temp'), 'fsprinter_ticket');
var text = '\x1B' + '@' + arg;
fs.writeFile(ticketPath, text, function (err) {
if (err) {
return console.log(err);
}
prinTicket(settings.getSync('printer.name'));
});
event.reply('send-to-printer', 'ok');
})