PrintService.ts 4.5 KB

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