Issue 1:
Electron Version:

4.0.7

Operating System:

Linux x86_64 4.20.x

Last Known Working Electron version::

3.0.9 (not tested afterwards)

Expected Behavior

When calling webContents.print and cancelling the native print dialog the callback should be called directly.

Actual Behavior

The callback is called after (during?) the window is closed/destroyed.

To Reproduce

const win = ...
win.webContents.print({ printBackground: true }, () => {
log('Printing finished')
// ...
})

Screenshots

The crash in the example happened because the window is destroyed and the callback is called, which normally removes stuff from the window.

Issue 2:
Electron version: 3.0.10

Operating System: macOS Mojave and Window 10

Expected Behavior

When we cancel the printing (button cancel in the print dialog) after a call to win.webContents.print(), the callback should return something.

Actual behavior

When we cancel the printing (button cancel in the print dialog) after a call to win.webContents.print(), the callback does return nothing.

To Reproduce

I want to print an HTML page generated by my electron application and saved to a file. The problem with the following code is : when the cancel button is clicked on the print dialog, the newly created window (win) is not closed because the print() callback is not called.

As it is an hidden window (the user cannot see the window which is still open and close it), each print make a new "ghost" window. Then when the application is closed (quit) on Windows 10, the "ghost" Windows are not closed and the application cannot be launch again (I use app.requestSingleInstanceLock()) without restart the computer or kill each "ghost windows" in the Windows task manager.. This part works fine on macOS as all the "ghost" windows are closed on quit (but this is still an issue since after 50 printing cancelled, we have 50 "ghost" windows)..

// code on the renderer process
import path from 'path'
import fs from 'fs'
import { remote } from 'electron'

print () {
const BrowserWindow = remote.BrowserWindow
const app = remote.app

const htmlFilePath = path.join(app.getPath('temp'), 'print-test.html')
const htmlContent = '<!doctype html><html lang="fr"><head><meta charset="utf-8"><title>Page title</title></head><body><div id="app"><h1>Hello</h1><p>Lorem ipsum dolor sit amet, nec ne ferri voluptatum. Paulo omnes vituperata qui ex. Ut dicat mediocrem signiferumque pri. Ad mel alterum tincidunt, vero alterum ei usu. Sit te semper consequuntur, alii quot omittam ea mei, in cum stet pertinacia maiestatis.</p><p>Ne mel alii hinc phaedrum. Mel cu ullum philosophia, numquam reprehendunt et mel. Ne pro deleniti torquatos, duo modus omnium ei, mollis expetenda an quo. Minimum accusamus pro eu. No tota pertinax duo, ad pri torquatos mediocritatem.</p></div></body></html>'

fs.writeFileSync(htmlFilePath, htmlContent)

let win = new BrowserWindow({ width: 800, height: 600, show: false }) // note that this is an hidden window !
win.loadURL('file://' + htmlFilePath)

win.on('closed', () => {
win = null
})

win.webContents.on('did-finish-load', function () {
win.webContents.print({}, function (success) {
if (success) {
console.log('print success')
}
// this is not called if the print is canceled (click on the cancel button in the print dialog)
win.close()
})
})
}

So I have no idea of how to close the window after a print cancel. Is it a bug that the callback is not called ? How can I workaround this problem ? Thanks

--

It could be linked with this issue #15568.