|
@@ -1,40 +1,95 @@
|
|
|
-import { printDirect, getDefaultPrinterName, getJob } from 'printer'
|
|
|
+import { v5 as UUIDv5 } from 'uuid'
|
|
|
+import { printDirect, getPrinters, getDefaultPrinterName, getJob } from 'printer'
|
|
|
|
|
|
-const requestPrint = ({data}) => {
|
|
|
- data = data.replace(/(data:[a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+.*,)/, '')
|
|
|
+const printQueue = []
|
|
|
|
|
|
- const buffer = Buffer.from(data, 'base64')
|
|
|
- const asciiData = buffer.toString('ascii')
|
|
|
+const REQUEST_PRINTER_NAME = 'request_printer_name'
|
|
|
+const SHOW_PRINT_STATUS = 'show_print_status'
|
|
|
|
|
|
- console.log(asciiData)
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} param0
|
|
|
+ */
|
|
|
+const requestPrint = (socket, request) => {
|
|
|
const defaultPrinter = getDefaultPrinterName()
|
|
|
|
|
|
+ socket.emit(SHOW_PRINTERS, getPrinters())
|
|
|
+ return
|
|
|
+
|
|
|
+ if (!defaultPrinter) {
|
|
|
+ socket.emit(SHOW_PRINTERS, getPrinters())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ const data = request.data.replace(/(data:[a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+.*,)/, '')
|
|
|
+ const buffer = Buffer.from(data, 'base64')
|
|
|
+
|
|
|
printDirect({
|
|
|
- data: asciiData,
|
|
|
+ data: buffer,
|
|
|
printer: defaultPrinter,
|
|
|
type: 'PDF',
|
|
|
success: jobId => {
|
|
|
const job = getJob(defaultPrinter, jobId)
|
|
|
+
|
|
|
+ console.log(job)
|
|
|
|
|
|
if (job.status.includes('PRINTING')) {
|
|
|
- console.log('imprimiendo')
|
|
|
- return
|
|
|
+ socket.emit(SHOW_PRINT_STATUS, {
|
|
|
+ status: 'printing',
|
|
|
+ printer: defaultPrinter
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
if (job.status.includes('PRINTED')) {
|
|
|
- console.log('impreso')
|
|
|
- return
|
|
|
+ socket.emit(SHOW_PRINT_STATUS, {
|
|
|
+ status: 'printed',
|
|
|
+ printer: defaultPrinter
|
|
|
+ })
|
|
|
}
|
|
|
-
|
|
|
- console.log(job)
|
|
|
},
|
|
|
error: e => {
|
|
|
- console.log(e)
|
|
|
+ socket.emit(SHOW_PRINT_STATUS, {
|
|
|
+ status: 'error',
|
|
|
+ printer: defaultPrinter
|
|
|
+ })
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} socket
|
|
|
+ * @param {*} request
|
|
|
+ */
|
|
|
+const doPrint = (socket, request) => {
|
|
|
+ const printerName = null
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} socket
|
|
|
+ * @param {*} request
|
|
|
+ */
|
|
|
+const printProcessor = (socket, request) => {
|
|
|
+ if (!request.print_directly) {
|
|
|
+ request.id = UUIDv5()
|
|
|
+ printQueue.push(request)
|
|
|
+
|
|
|
+ socket.emit(REQUEST_PRINTER_NAME, {
|
|
|
+ id: request.id
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ doPrint(socket, request)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} socket
|
|
|
+ */
|
|
|
export const printHandler = socket => {
|
|
|
- socket.on('request-print', requestPrint)
|
|
|
-}
|
|
|
+ socket.on('request_print', request => printProcessor(socket, request))
|
|
|
+}
|