<ul dir="auto">
<li><strong>Electron Version:</strong>
<ul dir="auto">
<li>4.0.7</li>
</ul>
</li>
<li><strong>Operating System:</strong>
<ul dir="auto">
<li>Linux x86_64 4.20.x</li>
</ul>
</li>
<li><strong>Last Known Working Electron version:</strong>:
<ul dir="auto">
<li>3.0.9 (not tested afterwards)</li>
</ul>
</li>
</ul>
<h3 dir="auto">Expected Behavior</h3>

<p dir="auto">When calling <code class="notranslate">webContents.print</code> and cancelling the native print dialog the callback should be called directly.</p>
<h3 dir="auto">Actual Behavior</h3>

<p dir="auto">The callback is called after (during?) the window is closed/destroyed.</p>
<h3 dir="auto">To Reproduce</h3>

<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const win = ...
win.webContents.print({ printBackground: true }, () =&gt; {
    log('Printing finished')
    // ...
})"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">win</span> <span class="pl-c1">=</span> ...
<span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-c1">webContents</span><span class="pl-kos">.</span><span class="pl-en">print</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-c1">printBackground</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'Printing finished'</span><span class="pl-kos">)</span>
    <span class="pl-c">// ...</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span></pre></div>


<h3 dir="auto">Screenshots</h3>

<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/22716132/53985690-98cd6c80-411c-11e9-9623-7dbfc0543b58.gif"><img src="https://user-images.githubusercontent.com/22716132/53985690-98cd6c80-411c-11e9-9623-7dbfc0543b58.gif" alt="print_callback" data-animated-image="" style="max-width: 100%;"></a></p>
<p dir="auto">The crash in the example happened because the window is destroyed and the callback is called, which normally removes stuff from the window.</p>
<ul dir="auto">
<li>Electron version: 3.0.10</li>
<li>Operating System: macOS Mojave and Window 10</li>
</ul>
<p dir="auto"><strong>Expected Behavior</strong><br>
When we cancel the printing (button cancel in the print dialog) after a call to <code class="notranslate">win.webContents.print()</code>, the callback should return something.</p>
<p dir="auto"><strong>Actual behavior</strong><br>
When we cancel the printing (button cancel in the print dialog) after a call to <code class="notranslate">win.webContents.print()</code>, the callback does return nothing.</p>
<p dir="auto"><strong>To Reproduce</strong></p>
<p dir="auto">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 (<code class="notranslate">win</code>) is not closed because the <code class="notranslate">print()</code> callback is not called.</p>
<p dir="auto">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 <code class="notranslate">app.requestSingleInstanceLock()</code>) 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)..</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// 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 = '&lt;!doctype html&gt;&lt;html lang=&quot;fr&quot;&gt;&lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;title&gt;Page title&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;div id=&quot;app&quot;&gt;&lt;h1&gt;Hello&lt;/h1&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;'

  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', () =&gt; {
    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()
    })
  })
}"><pre class="notranslate"><span class="pl-c">// code on the renderer process</span>
<span class="pl-k">import</span> <span class="pl-s1">path</span> <span class="pl-k">from</span> <span class="pl-s">'path'</span>
<span class="pl-k">import</span> <span class="pl-s1">fs</span> <span class="pl-k">from</span> <span class="pl-s">'fs'</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">remote</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'electron'</span>

<span class="pl-en">print</span> <span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos"></span> <span class="pl-kos">{</span>
  <span class="pl-k">const</span> <span class="pl-v">BrowserWindow</span> <span class="pl-c1">=</span> <span class="pl-s1">remote</span><span class="pl-kos">.</span><span class="pl-c1">BrowserWindow</span>
  <span class="pl-k">const</span> <span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-s1">remote</span><span class="pl-kos">.</span><span class="pl-c1">app</span>

  <span class="pl-k">const</span> <span class="pl-s1">htmlFilePath</span> <span class="pl-c1">=</span> <span class="pl-s1">path</span><span class="pl-kos">.</span><span class="pl-en">join</span><span class="pl-kos">(</span><span class="pl-s1">app</span><span class="pl-kos">.</span><span class="pl-en">getPath</span><span class="pl-kos">(</span><span class="pl-s">'temp'</span><span class="pl-kos">)</span><span class="pl-kos">,</span> <span class="pl-s">'print-test.html'</span><span class="pl-kos">)</span>
  <span class="pl-k">const</span> <span class="pl-s1">htmlContent</span> <span class="pl-c1">=</span> <span class="pl-s">'&lt;!doctype html&gt;&lt;html lang="fr"&gt;&lt;head&gt;&lt;meta charset="utf-8"&gt;&lt;title&gt;Page title&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;div id="app"&gt;&lt;h1&gt;Hello&lt;/h1&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;'</span>

  <span class="pl-s1">fs</span><span class="pl-kos">.</span><span class="pl-en">writeFileSync</span><span class="pl-kos">(</span><span class="pl-s1">htmlFilePath</span><span class="pl-kos">,</span> <span class="pl-s1">htmlContent</span><span class="pl-kos">)</span>

  <span class="pl-k">let</span> <span class="pl-s1">win</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-v">BrowserWindow</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-c1">width</span>: <span class="pl-c1">800</span><span class="pl-kos">,</span> <span class="pl-c1">height</span>: <span class="pl-c1">600</span><span class="pl-kos">,</span> <span class="pl-c1">show</span>: <span class="pl-c1">false</span> <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c">// note that this is an hidden window !</span>
  <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">loadURL</span><span class="pl-kos">(</span><span class="pl-s">'file://'</span> <span class="pl-c1">+</span> <span class="pl-s1">htmlFilePath</span><span class="pl-kos">)</span>

  <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'closed'</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-s1">win</span> <span class="pl-c1">=</span> <span class="pl-c1">null</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span>

  <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-c1">webContents</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'did-finish-load'</span><span class="pl-kos">,</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-c1">webContents</span><span class="pl-kos">.</span><span class="pl-en">print</span><span class="pl-kos">(</span><span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">success</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
      <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">success</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'print success'</span><span class="pl-kos">)</span>
      <span class="pl-kos">}</span>
      <span class="pl-c">// this is not called if the print is canceled (click on the cancel button in the print dialog)</span>
      <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">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</p>
<p dir="auto">--<br>
It could be linked with this issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="377425791" data-permission-text="Title is private" data-url="https://github.com/electron/electron/issues/15568" data-hovercard-type="issue" data-hovercard-url="/electron/electron/issues/15568/hovercard" href="https://github.com/electron/electron/issues/15568">#15568</a>.</p>