File size: 857 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const { BrowserWindow, ipcMain: ipc } = require( 'electron' );
const log = require( '../../lib/logger' )( 'desktop:printer' );

module.exports = function () {
	ipc.on( 'print', function ( event, title, contents ) {
		let printer = new BrowserWindow( { width: 350, height: 300, title: title } );

		log.info( `Printing contents '${ title }'...` );

		const file = 'data:text/html;charset=UTF-8,' + encodeURIComponent( contents );
		printer.loadURL( file );

		printer.webContents.on( 'dom-ready', function () {
			const options = { silent: false }; // ask the user for print settings
			setTimeout( function () {
				printer.webContents.print( options, ( success, error ) => {
					if ( ! success ) {
						log.error( 'Failed to print: ', error );
					}
				} );
			}, 500 );
		} );

		printer.on( 'closed', function () {
			printer = null;
		} );
	} );
};