-
Notifications
You must be signed in to change notification settings - Fork 15k
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
window.print() only works once #14705
Comments
👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can. To help make it easier for us to investigate your issue, please follow the contributing guidelines. |
@michaelsmitha works fine for me every time |
Right, it doesn't in v3.0.0-beta.13 |
@MarshallOfSound are we seeing this in v3.x |
Same here. Yes this is a bug in v3 on Windows. Printer works fine on a fresh restart, very inconsistent with following calls. Windows 10 |
Anyone know of any workarounds for the moment on this bug? |
I'm seeing this bug in 3.0.4, Windows 10. Works ok on macOS. Agree first print works after starting app. No printing thereafter. Unfortunately a show stopper for us. Downgrading to 2.0... |
Total showstopper for us, must downgrade to 2.0 |
Closest I can suggest is to use webContents.printToPDF(), save to temp file, then shell.openExternal(). Then user has to print the PDF. Not a great workaround but kinda sorta gets you there... |
Retested with Electron 4.0.0-beta.4 / Windows 10: app now crashes after clicking Print button on print dialog. |
I'm on electron 1.8.8, here is a bit of off-topic rambling, which hopefully would spark some ideas as to what the cause may or may not be. Before learning about ipc, I had some main-side print code:
electron.js
The actual behavior was happily blocking and synchronous for either a Cancel or Print choice in the print dialog. Windows and Ubuntu. Then I learned about ipc and refactored to use renderer-side code:
The observed behavior on Windows 10 is that the printDialog opens, then (likely before or) immediately after the user choice is made, the close method is executed, then (I imagine the blackbox as) printToPrinter or printToFile method is called, and creates a print job as seen in the printer's queue momentarily or does save a pdf file, but opening the pdf says something along the lines that it is empty or wrong, and the physical printer never spits out any paper. In my attempts to refactor with the correct synchronicity, I went back to the electron way still on the renderer-side,
Back on Ubuntu 18.04, the browser window closes before the print dialog is ever seen. Not sure if my event chain made it seem synchronous before, or the non-remote method is different than the remote one. Utilizing the callback,
The print dialog opened, [not sure /] I chose Cancel or Print to file maybe, the dialog closed, the window stayed open [/], and my entire machine froze, needing to be forcibly shut down. I was also running Slack in a Firefox tab at the time. I am on wifi and have about 10 network printers that do sometimes take a while to appear in the print dialog's device list. With no Slack tab open, this works as expected when the Print choice is made, but the callback does not execute if the Cancel choice is made. So if that is intended, I recommend changing the webContents documentation to refer to it as a successCallback instead of a callback with a success/error boolean argument. I also noticed that for example, Paint, on Windows 10, if you open two pngs, there can be two print dialogs open at the same time; never a problem on Ubunutu. But on Windows 10, if I spawn 2 browser windows which open printDialogs at the same time, only 1 dialog opens. |
electron 5.0.5 again this bug is there in 3.1.8 or 4.X.X wasn't |
Also happening in electron 5.0.7 .. Fyi To add. For my case, If I click OS: macOS Mojave Can it be the above PR was missed to be merged in 5.x.x versions ?! @brenca |
Happening in electron 5.0.8 |
Happening in electron 6.0.0 |
It's also in electron 5.0.6 👎 |
I guess we should open a new issue for this instead of commenting on an year old conversation ?! |
6.0.0 too |
After issuing window.print() inside an electron app, the app crashes after user has chosen "print" in the dialog box. It sometimes also crashes after user chooses Cancel. Windows 10. |
Application is crashing after print or save as pdf in v6.0.1 too. have you found any solution? |
Could we just open a new issue instead?? |
Was previously using electron version 6 and my processes crashed immediately after printing. I had to downgrade to electron 2.0.2. One thing i noticed is that in electron 2.0.2, i had to !-- Insert this line above script imports --> <script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>and also <script>if (window.module) module = window.module;</script>for most cases involving using Jquery scripts or Javascript. I thought about what if in Version 6 adding those scripts above could probably stop the process from crashing. You can try it out before downgrading, as for me i have a projects to deliver so i will just stick with my downgraded version as it works fine, at least for now and hopefully through deployment. |
This is a pretty old issue, but I am running into it on electron I have a workaround that appears to function in my case. You can spawn a new hidden const printRawHtml = html => {
const win = new BrowserWindow({ show: false });
win.loadURL(`data:text/html;charset=UTF-8,${
encodeURIComponent(`<html><body>${html}</body></html>`)
}`);
win.webContents.on('did-finish-load', () => {
win.webContents.executeJavaScript(
'window.print(); setTimeout(() => window.close());',
);
});
}; This of course only works if you are easily able to retrieve the necessary css/html from the page you would like to be printed. This was only tested on Ubuntu 19.04. |
This is still an issue in 7.0.0 |
Yep, I can reproduce this in 7.0.0 on Windows |
Any updates for this bug? I am in production and it's still in 7.0.0 on windows... |
Having this issue in electron v7.1.1 on Windows 7. Printer selection window is displayed after the first try, but printing only works the first time. Steps to reproduce like in original Post with a npm install electron@latest before starting. |
Still an issue in electron v7.1.2 on Windows 10. Same behavior as described by @InfiniteLukeOne |
Take a look at #21195. There's a workaround in the comments which worked for me. |
I am facing exactly the same issue. The only workaround that worked for me was downgrading to electron version 3. |
|
Is this issue resolved? I'm facing same issue in Electron 10.1.0 |
I face the same issue in Electron 9.1.2. on MacOs with the |
I got around this by capturing the screen to pdf, and reprinting that. It
is a small use case but worked for my work flow
…On Fri, Sep 4, 2020, 9:08 AM Karel Mácha ***@***.***> wrote:
I face the same issue in Electron 9.1.2. on MacOs with the printToPDF
method
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#14705 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABFU3KA7NPL5TIOMS5H6U3DSEDRFDANCNFSM4FWDTEZQ>
.
|
It was solved for me in Electron 9.0.5 when I last implemented this in an Electron version of the WikiMed app. There I can print as many times as I like without a crash (just tested the same app again). I hope they haven't re-introduced the bug since 9.0.5. |
I just tried with electron 9.0.5. and I was able to print multiple times to PDF without any problems. |
Can someone check on Electron 10.1.0, I'm facing similar kind of issue, opened separate issue and it's tagged as bug #25327 |
A work around for previous versions, based On @TristonJ answer // Create a new window to handle printing
const printWin = new BrowserWindow({ show: false });
// Load current window url
printWin.loadURL(BrowserWindow.getFocusedWindow().webContents.getURL());
// Make sure the content fully loaded
printWin.webContents.on('did-finish-load', () => {
printWin.on("close",()=>{
console.log("print window closed")
})
// Handle printing logic and notify the mainWindow
printWin.webContents.print(printingOptions,(success, err) => {
if(success){
mainWindow.webContents.send("print:success")
printWin.close()
}else{
mainWindow.webContents.send("print:failed",{error:err})
printWin.close()
}
console.log('err :>> ', err);
});
}); When printing on the same window, |
node_modules/.bin/electron --version
: v3.0.0-beta.13node_modules/.bin/electron --version
on last known working Electron version (if applicable): ?Expected Behavior
window.print() should print the window on each subsequent call
Actual behavior
window.print() will work once, but fail from there on. This looks like a regression of #1532?
The device selection always comes up successfully. I've tried printing both to "Microsoft Print to PDF", which only gives you the "save location" dialog once, and my local printer, which will print once, then does nothing.
To Reproduce
Create a barebones electron app that can call
window.print()
as desired. Repository included.clone: https://github.com/michaelsmitha/print-test
npm install
.\node_modules\electron\dist\electron.exe .
Attempting to print the window after the first time fails.
The text was updated successfully, but these errors were encountered: