import { BrowserWindow, ipcMain } from 'electron'; import path from "path"; export function printUtils(params) { return new Promise(async (resolvePrint, rejectPrint) => { let subMainWindow = new BrowserWindow({ // show: false, webPreferences: { nodeIntegration: true, // For electron >= 4.0.0 contextIsolation: false, webSecurity: false, enableRemoteModule: true } }); // 加载打印的html文件 subMainWindow.loadFile(path.join(__dirname, "../public/print.html")); subMainWindow.webContents.on('did-finish-load', async (res) => { subMainWindow.webContents.openDevTools(); console.log('网页加载完成', res) subMainWindow.webContents.send('getParams', params) return let data = [] return renderPrintDocument(subMainWindow, data) .then(async (result) => { // let width = Math.ceil((result.width) * 264.5833); let height = Math.ceil((result.height + 60) * 264.5833); console.info('height', result, height); subMainWindow.webContents.print({ silent: true, margins: { marginType: 'none' }, printBackground: false, deviceName: '', copies: 1, }, (success) => { if (success) { resolvePrint({ type: 'success' }) } subMainWindow.close(); }) }) .catch(err => console.warn(33, err)) }) ipcMain.on('printStart', () => { console.log('开始打印') subMainWindow.webContents.print({ silent: true }) }) }) }