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

Please, make printing work with Electron ! #17523

Closed
popod opened this issue Mar 23, 2019 · 43 comments
Closed

Please, make printing work with Electron ! #17523

popod opened this issue Mar 23, 2019 · 43 comments

Comments

@popod
Copy link
Contributor

popod commented Mar 23, 2019

Problem Description

There are many printing problems since Electron 3 and this prevents people who need a working print solution to use Electron 3, 4 or 5 !

So please, can you take a look at all this printings issues and trying to fix it ? We are a lot of developers waiting for this since a long time..

Thanks a lot for your work ! And I hope you could give us some feedback about this.

cc: @codebytere @MarshallOfSound @nornagon

Related issues

All this issues seems related to webContents.print() which seems to be buggy:

Other printing issues:

(Non-exhaustive list)

@mojason-81
Copy link

mojason-81 commented Apr 12, 2019

Just thought I'd leave some more info here. I'm having similar issues on v4.1.4 (and 4.0.x before). The print dialog will open, but only intermittently. It will open several times in a row, then not open for a while and maybe open again once or twice, but ultimately seems to stop opening altogether.

EDIT: Also, tried to just force silent printing by passing true as that option to the print function along with a device name. This seems to have no effect and the print dialog still opens on the first try and continues to be intermittent. See also: #16362

@brenca brenca self-assigned this Apr 17, 2019
@baconbrad
Copy link
Contributor

This is out of my abilities to fix. I see it was self assigned by @brenca 2 weeks ago. I am just curious if there is any progress on these issues. Particularly in regards to using a deviceName and having webContents.print() work as expected. Until they are resolved my projects and many others will be blocked to Electron 3.X.X. I apologize I have nothing to contribute to these issues and I don't want this comment to come off as unappreciative nagging. I am just interested to see how they are coming along. Thank you.

@baconbrad
Copy link
Contributor

Just noticed this. https://github.com/electron/electron/blob/96b32a814ceda6818f7b9e1138f7468e7767cda2/patches/common/chromium/printing.patch

Looks like the issues are getting addressed. Thanks, @brenca!

@gozzoo
Copy link

gozzoo commented May 29, 2019

It doesn't seem so 😞

PR descrption says:

Updating Chromium to 964c4bca8de5c320534d95606c861 (lkgr).
Fixes #18409

@codebytere codebytere self-assigned this May 29, 2019
@popod
Copy link
Contributor Author

popod commented Jun 25, 2019

Hooray ! It seems that printing problems with Electron will soon be only old memories!

Some problems have already been fixed in Electron 5.0.5 and others seem to be soon. Thanks to @codebytere for her work!

@JonaMX
Copy link

JonaMX commented Jul 1, 2019

I just tested 5.0.5 and silent print still not working

@codebytere
Copy link
Member

@JonaMX yes, that is expected. It has not been backported to that release line yet and remains in flight; if it had you would have been able to see it in the release notes.

@craftzdog
Copy link

It still does not work in 5.0.6.
I tried webview.getWebContents().print as a workaround but did not work.
A print dialog properly opens but nothing is produced.
When I chose 'Save as PDF', it outputs an empty file.
webView.printToPDF does nothing.

@baconbrad
Copy link
Contributor

@craftzdog As expected. According to the releases notes this change was not pushed in 5.0.6.

@craftzdog
Copy link

@baconbrad thx for letting me know. hope it will be pushed to 5.x soon!

@ebmbook
Copy link

ebmbook commented Jul 12, 2019

Just in case this helps someone, the only stable version which works with printing is v3.0.10.

@gozzoo
Copy link

gozzoo commented Jul 13, 2019

@ebmbook yes, it seems that the supposed fix has not been ported to any release branch.

Actually this is an umbrella issue covering many more specific problems and without a chance to test this fix I'm very spectical that it fixes all of them. These issues have been dragging for quite a while and it's obvious that prining has a very low priority in the development plans.

We are also stuck with v3.0.x and I don't see our project upgrading to a newer electron version any time soon.

@misbeliever
Copy link

Same here stuck on v3 because silent printing still does not work correctly on any newer version.

@codebytere
Copy link
Member

codebytere commented Aug 8, 2019

@JonaMX v5.0.7 fixes silent printing.

#19598 was also recently merged and fixes a slew of issues, which y'all can test in v7.0.0-beta.2

I'll be porting portions of that fix back as soon as i can for v6 and v5.

@JonaMX
Copy link

JonaMX commented Aug 8, 2019

Thanks @codebytere I'll test this out and let you know how it went.

@baconbrad
Copy link
Contributor

Just tested our POS with v6.0.10 and most everything works with the exception when we specify a deviceName. Just waiting for either the fixes for PR #19598 to make it to v6 or a stable release of v7. I appreciate the hard work of everyone that addressed the issues. Thank you! Looking forward to ditching v3 for good.

@barbalex
Copy link

barbalex commented Sep 25, 2019

Don't know if this is the right place to add this but printBackground: true seems to be completely ignored inside webContents.print.

It does work when using webContents.printToPDF.

This has been rather consistent for us since we use electron (v3).

@cxyao
Copy link

cxyao commented Oct 14, 2019

We also meet the same problem under v5.0.11 @barbalex
webContents.print({ printBackground: true } )not work.
@codebytere @popod could you help us ?

@seksitha
Copy link

seksitha commented Dec 3, 2019

@JonaMX v5.0.7 fixes silent printing.

#19598 was also recently merged and fixes a slew of issues, which y'all can test in v7.0.0-beta.2

I'll be porting portions of that fix back as soon as I can for v6 and v5.

I test the beta and it works. Thank you very much. no more printToPdf and say goodby to exec pdfToPrinter.exe with a long lagging. the process of printing the barcode label is fast now.

@codebytere
Copy link
Member

Hey folks - i still monitor this issue so if you see printing issues please amalgamate them here so i can continue to chip away at printing problems!

@barbalex
Copy link

@codebytere how about #17523 (comment)?

@codebytere
Copy link
Member

codebytere commented Jan 26, 2020

@barbalex i'll need some more info; what platform(s) is this occurring on? do you have a simple repro you know triggers the issue (ex. so i can be sure i'm testing with the config closest to the one you're having issues with)

@seksitha
Copy link

I just had a recent bad experienced with windows 7 with Intel Celeron CPU. it takes 10+ seconds to spool the SVG barcode generated by Jsbarcode. I now in electron 8. My workaround is by using printhtml.exe by sending HTML string write it to file and print. Still not perfect for my printing job and that might be a deal-breaker to some people.

@barbalex
Copy link

@codebytere
Am have developed two electron apps so far. With all the electron versions since v2, up to 8beta. As far as I can remember this has always been the case but there were so many more important printing problems that I am not 100% sure.

I can see this in dev and prod mode on Windows 10 ans MacOS (whatever the newest version is).

Unfortunately I do not know how to build a simple repro.

@codebytere
Copy link
Member

codebytere commented Jan 28, 2020

@barbalex this unfortunately seems to be an issue with chrome itself; you can see this if you create a simple webpage:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <style>
      body {
        background-color: lightgreen;
        -webkit-print-color-adjust: exact;
      }
    </style>
  </head>
  <body>
    <h1>Hello World!</h1>
    <script>
    </script>
  </body>
</html>

and then open it in chrome and try to print it. That too is bereft of background colors.

@barbalex
Copy link

barbalex commented Jan 28, 2020

@codebytere

Yes, but:

  1. You can tell Chrome in the print settings to print "Hintergrundgrafiken" (not sure what the name is that chrome uses in English).
  2. You can set this value in css to force Chrome to print background-images and -colors:
@media print {
  -webkit-print-color-adjust: exact;
}

Edit: I never tried the css trick in electron. Just learned it a few weeks ago in a web project. I guess it works in electron too. But it would be nice if printBackground: true would work as advertised.

Edit2: I can confirm that -webkit-print-color-adjust: exact; works in electron as well as in the browser 😄

@Hedwig188
Copy link

Hedwig188 commented Feb 26, 2020

I ran into an issue when calling window.webContents.print() on a new opened window, it just outputs an empty file in v5.0.7+window10. I also tried to pass the options, but it didn't help. window.webContents.printToPDF() has the same issue. Here is my example code.

window.webContents.on('did-finish-load', function () {
			// Use default printing options
			window.webContents.print({}, function (response) {
				console.log(response);

			});

Is there any workaround for this for v5.0.7?

@gozzoo
Copy link

gozzoo commented Feb 26, 2020

@Hedwig188 I had this problem when trying to print immediately after opening a windows. It turned out that the events 'did-finish-load' and 'ready-to-show' sometimes come in different order.

I did it like this:

  let p1 = new Promise((resolve) => win.webContents.on('did-finish-load', resolve))
  let p2 = new Promise((resolve) => win.on('ready-to-show', resolve))
  Promise.all([p1, p2]).then(printCallback)

@Hedwig188
Copy link

@gozzoo I tried your solution, but still got the empty file as the result.

@gozzoo
Copy link

gozzoo commented Feb 27, 2020

@Hedwig188 you may try saving the html before printing, to make sure that there is actually something ready to be printed. If the page contents gets generated dynamically it's quite possible that at the time those events get fired the page is still empty.

@jriffel
Copy link

jriffel commented Jun 1, 2020

@codebytere - Thanks for your printing contributions ... seems like there was a mountain to climb here over the last few years.

On Windows the option for printBackground is still ignored if you allow the user to select the printer. If you print silent and specify the printer printBackground works as advertised. Thankfully your comments above did provide me a workaround which allowed us to remove our own printer selection dialog to work around this:

body {
  -webkit-print-color-adjust: exact;
}

@yetianyue
Copy link

Just in case this helps someone, the only stable version which works with printing is v3.0.10.

Thx,At last it worked

@gozzoo
Copy link

gozzoo commented Sep 10, 2020

Thx,At last it worked

@yetianyue what worked and what didn't work before that?

I'm also stuck with v.3.0 and just don't want to spend the time and energy to test if printing works with each new version. I've been keeping my hopes up for v.10.

Has anywone tested printing with v10?

@orionlu0916
Copy link

orionlu0916 commented Sep 11, 2020

Thx,At last it worked

@yetianyue what worked and what didn't work before that?

I'm also stuck with v.3.0 and just don't want to spend the time and energy to test if printing works with each new version. I've been keeping my hopes up for v.10.

Has anywone tested printing with v10?

For my case, the current highest working version is 2.0.18,
In my use case, I use pug to render a html file, create a pdf file, and then print the PDF with silent mode.
weirdly, with the "exact same code", if I use electron version >= v3,
the entire generated html content will be comment out with extra unexpected code.
besides, the printing task will only pop in the printing task manager queue for about 1 sec and then disappear.

@squalle0nhart
Copy link

Thx,At last it worked

@yetianyue what worked and what didn't work before that?

I'm also stuck with v.3.0 and just don't want to spend the time and energy to test if printing works with each new version. I've been keeping my hopes up for v.10.

Has anywone tested printing with v10?

I've tested with latest electron 10. Print functions working fine!

@jngbng
Copy link

jngbng commented Nov 11, 2020

@gozzoo I've tested with electron 10.1.5 and printing iFrame is still not working.

document.querySelector("#print-iframe").contentWindow.print()

freezes after a print job is queue.

@SUH11
Copy link

SUH11 commented Nov 17, 2020

@gozzoo I've tested with electron 10.1.5 and printing iFrame is still not working.

document.querySelector("#print-iframe").contentWindow.print()

freezes after a print job is queue.

I have same problem in electron 10.1.5 and mac

@lynxtaa
Copy link
Contributor

lynxtaa commented Nov 17, 2020

We've "solved" all printing issues by discarding electron printing altogether.

  1. By running Gotenberg in Docker we can convert HTML pages to PDF. Also this method solves missing page numbers at the bottom of the page: https://thecodingmachine.github.io/gotenberg/#html.header_and_footer)
  2. We pack our application with a PDF Printer. SumatraPDF in our case.

So when user clicks Print we convert HTML to PDF and just open resulted PDF in SumatraPDF. This solves missing print preview issue.

Also we can print in silent mode by supplying -silent to SumatraPDF.

I know it's a very hacky solution but I hope it helps.

EDIT:
Another, less reliable, way is running a web-server in the main process with some handler for GET /print request. When user clicks Print you can run system's default browser via shell.openExternal('http://localhost:3000/print') and return HTML for printing:

<body>
  ...
  <script>
    window.print()
  </script>
</body>

@ckerr
Copy link
Member

ckerr commented Nov 17, 2020

It looks like the issues in the description have all either been fixed by @codebytere or are referring to versions of Electron that are no longer supported. Moreover, a long-running thread with the topic "Please, make printing work with Electron !" is probably not the best place to report ongoing issues, so I'm going to close this issue.

The project does take printing seriously. If anyone is still seeing issues in supported versions of electron -- currently 9, 10, and 11 -- then I encourage you to report them as standalone issues. Thank you!

Also, in the spirit of the devs here who have shared tips and tricks with each other, you may want to check out the project's new Discord server! It's a good place to chat with other developers who are using Electron.

@ckerr ckerr closed this as completed Nov 17, 2020
@ibalaji777
Copy link

Electron missing font while print

i tested electron 3.1.10 working perfecly in developement
but i am facing issue only in production

@baconbrad
Copy link
Contributor

@ibalaji777 3.X is too out of date. A fix will never be backported since it is unsupported. You will need to use a later version. Unfortunately later versions are having regression issues. 11.X is still in the support scope and seems to be working fine for most printing issues though.

@exander77
Copy link

@baconbrad In what version it realiably prints? How to print PDFs as images?

@biswajit123
Copy link

I have a problem when working with win.webContents.printToPDF, pdf is getting generated but the text (color) is getting faded instead of my css text color, it is looks like a Gray color text with some opacity. has anyone faced this kind of issue before ? Can you please share any solutions?

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests