PrintService.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { printRecordService } from './database/PrintRecordService'
  2. import { getMainWindow } from 'ee-core/electron/window'
  3. import { logger } from 'ee-core/log'
  4. import { BrowserWindow } from 'electron'
  5. import {Event} from '../utils/Event'
  6. import {PrintEvent} from "../event/PrintEvent";
  7. import {Channel} from "../channel/Channel";
  8. import {PrintTaskQueue} from "../utils/PrintTaskQueue";
  9. import {randomUUID} from "node:crypto";
  10. /**
  11. * 打印服务
  12. */
  13. class PrintService {
  14. private static queue = new PrintTaskQueue()
  15. constructor() {
  16. }
  17. _init() {
  18. Event.on(PrintEvent.PRINT_DATA, async (res) => {
  19. if (res instanceof Array) {
  20. for (let print of res) {
  21. PrintService.queue.enqueue({taskId: randomUUID(), data: print})
  22. }
  23. }
  24. })
  25. this.printLoop()
  26. }
  27. printLoop() {
  28. new Promise(async (resolve, reject) => {
  29. try {
  30. while (!PrintService.queue.isEmpty()) {
  31. getMainWindow().webContents.send(Channel.PRINT_TASK_LIST, PrintService.queue.list())
  32. const task = PrintService.queue.dequeue()
  33. if (task) {
  34. // @ts-ignore
  35. await this.printData(task.data)
  36. }
  37. await new Promise(resolve2 => setTimeout(resolve2, 1000))
  38. resolve(true)
  39. }
  40. } catch (e) {
  41. logger.error('print loop ex ---> ', e)
  42. reject(e)
  43. }
  44. }).then((res: any) => {
  45. logger.info("print then --->", res)
  46. }).catch((e: any) => {
  47. logger.error('print catch ex ---> ', e)
  48. })
  49. }
  50. /**
  51. * 获取本地打印机列表
  52. */
  53. async listLocalPrinters(): Promise<Electron.PrinterInfo[]> {
  54. return getMainWindow().webContents.getPrintersAsync();
  55. }
  56. async printData(data: any) {
  57. return new Promise(async (resolve, reject) => {
  58. getMainWindow().webContents.send(Channel.PRINT_START)
  59. const printWindow = new BrowserWindow({ show: false });
  60. const printData = `<html><body>${data.html}</body></html>`
  61. getMainWindow().webContents.send(Channel.PRINT_DATA, printData)
  62. await printWindow.loadURL(`data:text/html, ${printData}`);
  63. printWindow.webContents.on('did-finish-load', () => {
  64. getMainWindow().webContents.send(Channel.PRINT_PROGRESS)
  65. printWindow.webContents.print({
  66. silent: true,
  67. printBackground: true,
  68. deviceName: data.deviceName ,
  69. margins:data.margins||{
  70. marginType :'none'
  71. },
  72. landscape:data.landscape||false,
  73. copies:data.copies||1,
  74. dpi:data.dpi,
  75. header:data.header,
  76. footer:data.footer,
  77. pageSize:data.pageSize //A4,Legal | {height:}
  78. }, (success: boolean, failureReason: string) => {
  79. logger.info('打印结果:', success, failureReason)
  80. getMainWindow().webContents.send(Channel.PRINT_RESULT, {
  81. success,
  82. failureReason
  83. })
  84. printRecordService.add({
  85. printerName: data.deviceName,
  86. printHtml: data.html,
  87. printStatus: success ? 'success' : 'failure',
  88. failureReason: failureReason
  89. })
  90. if (success) {
  91. resolve(true)
  92. }else {
  93. reject(failureReason)
  94. }
  95. printWindow.close();
  96. }); // 静默打印
  97. });
  98. })
  99. }
  100. }
  101. PrintService.toString = () => '[class PrintService]'
  102. const printService = new PrintService()
  103. export {
  104. PrintService,
  105. printService
  106. }