| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { printRecordService } from './database/PrintRecordService'
- import { getMainWindow } from 'ee-core/electron/window'
- import { logger } from 'ee-core/log'
- import { BrowserWindow } from 'electron'
- import {Event} from '../utils/Event'
- import {PrintEvent} from "../event/PrintEvent";
- import {Channel} from "../channel/Channel";
- /**
- * 打印服务
- */
- class PrintService {
- constructor() {
- }
- _init() {
- Event.on(PrintEvent.PRINT_DATA, async (res) => {
- await this.printData(res)
- })
- }
- /**
- * 获取本地打印机列表
- */
- async listLocalPrinters(): Promise<Electron.PrinterInfo[]> {
- return getMainWindow().webContents.getPrintersAsync();
- }
- async printData(data: any) {
- getMainWindow().webContents.send(Channel.PRINT_START)
- const printWindow = new BrowserWindow({ show: false });
- const printData = `<html><body>${data.html}</body></html>`
- getMainWindow().webContents.send(Channel.PRINT_DATA, printData)
- await printWindow.loadURL(`data:text/html, ${printData}`);
- printWindow.webContents.on('did-finish-load', () => {
- getMainWindow().webContents.send(Channel.PRINT_PROGRESS)
- printWindow.webContents.print({
- silent: true,
- printBackground: true,
- deviceName: data.deviceName ,
- margins:data.margins||{
- marginType :'none'
- },
- landscape:data.landscape||false,
- copies:data.copies||1,
- dpi:data.dpi,
- header:data.header,
- footer:data.footer,
- pageSize:data.pageSize //A4,Legal | {height:}
- }, (success: boolean, failureReason: string) => {
- logger.info('打印结果:', success, failureReason)
- getMainWindow().webContents.send(Channel.PRINT_RESULT, {
- success,
- failureReason
- })
- printRecordService.add({
- printerName: data.deviceName,
- printHtml: data.html,
- printStatus: success ? 'success' : 'failure',
- failureReason: failureReason
- })
- printWindow.close();
- }); // 静默打印
- });
- }
- }
- PrintService.toString = () => '[class PrintService]'
- const printService = new PrintService()
- export {
- PrintService,
- printService
- }
|