Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron 4.0.0: webContents.print() must be called twice or with an empty object #16219

Closed
c3er opened this issue Dec 30, 2018 · 13 comments
Closed
Assignees
Labels

Comments

@c3er
Copy link

c3er commented Dec 30, 2018

  • Output of node_modules/.bin/electron --version: v4.0.0
  • Operating System (Platform and Version): Windows 10 64 Bit, Version 1809
  • Output of node_modules/.bin/electron --version on last known working Electron version (if applicable): v3.0.13

Expected Behavior

If I call webContents.print(), the print dialog shall open. If I give a callback function, the callback shall be called.

Actual behavior

I must call webContents.print() twice before the print dialog opens. There is a workaround: If I give an empty object as parameter, the behavior is as expected: webContents.print({})

Additionally, I observed that the callback, if given, gets not called. This works also as expected under Electron 3.0.13.

To Reproduce

I made a repository with multiple examples to the described behavior.

To clone the repository:

$ git clone https://github.com/c3er/electronPrintingIssue.git

All examples should start after typing the following commands in their respective directories:

$ npm install
$ npm start

You can either click File -> Print in the main menu or press Ctrl+P - behavior is the same.

Additional Information

Explanation to the examples:

  • 01_expected_3.0.13: This shows the expected behavior under Electron 3.0.13. I press Ctrl+P and the printing dialog appears immediately.
  • 02_bug_4.0.0: This shows the described bug under Electron 4.0.0. I must press Ctrl+P twice before the dialog opens. The debugger (VS Code) shows that the function gets called every time.
  • 03_workaround_4.0.0: This shows the described workaround under Electron 4.0.0 by giving an empty object as parameter.
  • 04_callback-called_3.0.13: As expected, a given callback gets called and gives some output to the console.
  • 05_callback-not-called_4.0.0: The given callback seems not to be called. There is no output in the console.
c3er added a commit to c3er/mdview that referenced this issue Dec 31, 2018
Notes:

Electron version 4.0.0 has an issue regarding printing. See electron/electron#16219

The whole file "package.json" is marked as changed because of a change in .gitattributes (commit 1817420)
@javan javan added 4-2-x bug/regression ↩️ A new version of Electron broke something platform/windows labels Dec 31, 2018
@javan
Copy link
Contributor

javan commented Dec 31, 2018

I must call webContents.print() twice before the print dialog opens.

Confirmed. Additionally, I'm seeing that after the dialog opens once it won't open again until relaunching.

@javan
Copy link
Contributor

javan commented Jan 1, 2019

Just tested on macOS (10.14.2) and webContents.print() doesn't work at all. The print dialog never opens.

@javan
Copy link
Contributor

javan commented Jan 1, 2019

Did some more testing on macOS. Printing works as expected in the 4-0 betas up through 4.0.0-beta.9. Doesn't work in 4.0.0-beta.10, 4.0.0-beta.11, or 4.0.0.

#15976 was introduced in 4.0.0-beta.10 so the issue mostly likely lies somewhere in #15023 or #15143.

@bengotow
Copy link
Contributor

bengotow commented Jan 1, 2019

Hey folks—just wanted to add, I had this problem as well but was able to work around it by using window.print() instead (which seems to work fine.) Unfortunately you don't get a callback in that case...

@abdellaui
Copy link

does anyone have another clear solution as calling webContents.print() twice or window.print()?

@javan
Copy link
Contributor

javan commented Jan 4, 2019

Here's my current workaround:

- contents.print()
+ contents.executeJavaScript("window.print()")

@takkiraz
Copy link

takkiraz commented Jan 4, 2019

@javan your current workaround works, but is there another way for printing silent.

@javan
Copy link
Contributor

javan commented Jan 4, 2019

The window.print() API doesn't allow that, AFAIK. You could try using Electron v4.0.0-beta.9 or earlier until this issue is fixed.

@nitsakh
Copy link
Contributor

nitsakh commented Feb 21, 2019

Closing as this appears fixed in latest electron 5 beta. Feel free to reopen otherwise.

@maplemike
Copy link

Silent printing still fails in latest beta as mentioned in #16193

@maikonmatheus
Copy link

maikonmatheus commented Jun 13, 2019

Silent printing still fails in Electron 5.0.3. I had to downgrade to electron@3.1.8 for use the silent print.

@Catlite91
Copy link

Silent printing still fails in Electron 5.0.6.

@popod
Copy link
Contributor

popod commented Jul 4, 2019

@Catlite91 The backport which fix the "silent printing" issue is here #19038 Follow this PR to get notified when it will be merged into 5.x.x.

luxuereal pushed a commit to luxuereal/md_viewer_electron that referenced this issue Jun 16, 2023
Notes:

Electron version 4.0.0 has an issue regarding printing. See electron/electron#16219

The whole file "package.json" is marked as changed because of a change in .gitattributes (commit 1817420965a1e57bf27603eb9def9ba0ed41d029)
luxuereal added a commit to luxuereal/md_viewer_electron that referenced this issue Dec 12, 2023
Notes:

Electron version 4.0.0 has an issue regarding printing. See electron/electron#16219

The whole file "package.json" is marked as changed because of a change in .gitattributes (commit 1817420965a1e57bf27603eb9def9ba0ed41d029)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants