tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[feat] Provide print API

Open alimehasin opened this issue 3 years ago • 23 comments

Describe the problem

Tauri lacks a print API and this make it very difficult for electron based apps that perform lots of work with printers to migrate to Tauri, there are many use cases

  1. Unable to print a page if it is hidden visible: false (window.print() triggered automatically after page load)
  2. Unable to close the page after the printing dialog is closed (I haven't found any way to know if the dialog still open)
  3. Unable to perform silent printing

Describe the solution you'd like

Provide print API

Alternatives considered

At least I hope to see an event the get fired when the printing dialog closed

Additional context

I have seen Print to Printer and related options checked in this issue But electron has rich API regarding to printing comparing to Tauri

alimehasin avatar Aug 12 '22 08:08 alimehasin

Unable to close the page after the printing dialog is closed (I haven't found any way to know if the dialog still open)

It turns out that there is a JS event called afterprint that will solves this problem, you can read more about it here

alimehasin avatar Aug 18 '22 07:08 alimehasin

@alimehasin how this event afterprint will fix the printing in tauri ? i am very curious to know because i am facing this problem for very long , i am not able to launch my application because of printin . many people are trying to figure out how this process will help in providing print solution for tauri

how this event will tell how may printers are connected to the system? how this event tell which printer is default printer how to stop print dialog in chrome how to adjust the margin and custom print settings by this event ?

rizwan92 avatar Dec 09 '22 18:12 rizwan92

Hello @rizwan92, the afterprint event won't fix these issues, it will only fix one problem which is

Unable to close the page after the printing dialog is closed (I haven't found any way to know if the dialog still open)

As quoted in my previous comment

alimehasin avatar Dec 09 '22 18:12 alimehasin

I do not know why this issue's priority is medium. It is a very essential part of building a desktop application. I really want to launch my application using Tauri

rizwan92 avatar Dec 09 '22 18:12 rizwan92

@rizwan92 Tauri is an open source project depending on it's contributors. If you need this feature urgently, maybe you can help get the ball rolling?

For example by finding out the cross platform support by the underlying webviews for printing, and seeing what the common ground is?

Referencing https://tauri.app/v1/references/webview-versions/ if you need more insight in the webviews.

Beanow avatar Dec 09 '22 19:12 Beanow

I am looking forward to the silent printing API, because my app needs this API to print small tickets and price tags

ihekui avatar Jan 09 '23 14:01 ihekui

@Beanow I could not find any cross-platform solution for webviews. I tried writing my own solution in rust but it is way too hard for me I think I need to practice more rust first to get it done. i will open the source code so other can get help and contirbute

rizwan92 avatar Jan 13 '23 06:01 rizwan92

Now it's stable: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1331

noearaujo avatar Mar 02 '23 08:03 noearaujo

Now it's stable: MicrosoftEdge/WebView2Feedback#1331

Let me understand, is this functionality not yet available in Tauri? Is the problem currently unresolved?

Bukmopbl4 avatar Mar 10 '23 18:03 Bukmopbl4

Correct, it's still unresolved. This is also why this GitHub issue is still open :)

FabianLars avatar Mar 11 '23 09:03 FabianLars

I opened a tracking issue in our plugin repo where this feature will be implemented at some point: https://github.com/tauri-apps/plugins-workspace/issues/293. Please do not take this as a "We'll implement this feature now", this is just me trying to get the ball rolling.

FabianLars avatar Mar 31 '23 15:03 FabianLars

To perform a silent print, you can use plugin Tauri Plugin Printer. However, currently, it only supports Windows.

alfianlensundev avatar Jun 15 '23 03:06 alfianlensundev

要执行静默打印,您可以使用插件 Tauri 插件打印机。但是,目前,它仅支持Windows。

However, it must send files in PDF format, and its printing function is not as good as "electron".

wcldyx avatar Sep 23 '23 05:09 wcldyx

Is there a good solution now? I need to silently print an entire long webpage.

wcldyx avatar Sep 23 '23 05:09 wcldyx

The only thing keeping my team away from Tauri is this issue, is there any ETA, attempt or desire to actually implement this?

juancarlosgzs avatar Dec 17 '23 11:12 juancarlosgzs

ETA?

No.

attempt?

Nothing recently that i know of.

desire?

Yes, otherwise we would close this issue.

FabianLars avatar Dec 17 '23 12:12 FabianLars

Is there any solution for this now , Any plugins ?

I think Webview2 actually have Methods For this : webviewPrint

dev-itzarun avatar Feb 27 '24 08:02 dev-itzarun

Ok I think I'm kinda stucked by this lol.

Please, is it at least possible to use native window.print() with a html visible content ?

Lerado avatar Mar 05 '24 13:03 Lerado

As far as i know window.print() should work.

FabianLars avatar Mar 05 '24 13:03 FabianLars

As far as i know window.print() should work.

Ok I think that would be a workaround for now. Thank you for the good work there. Glad I bet on Tauri when it started back in the days xD.

Lerado avatar Mar 05 '24 13:03 Lerado

any progress on this guy? this seem to be critical problem already?

dnguyenfs avatar Jun 12 '24 02:06 dnguyenfs

This is really important, I can't use tauri until this is developed

bforbiggy avatar Jun 21 '24 21:06 bforbiggy

I'm concerned about this issue as well. For now, where I need to use silent printing, I use Electron. As far as I know, you can print using window.print() if the standard print dialog doesn't bother you. You could also go the extra mile with POS commands on the printer or write a printing module in another language, integrate it into a Tauri binary, and pass the necessary data for printing to it. All options seem to be a bit of a hassle if you need to print HTML receipts.

Bukmopbl4 avatar Jun 21 '24 22:06 Bukmopbl4

There is an issue generating PDF on webkitgtk, it seems not really possible given the upstream bug https://bugs.webkit.org/show_bug.cgi?id=212814

I started an implementation of print options, but PDF generation might be difficult to get where webkitgtk is used.

mildred avatar Jul 17 '24 10:07 mildred

I need this api its really important, I hope this gets resolved as soon as possible.

ohellel avatar Jul 20 '24 15:07 ohellel

For now I workaround this by including node and puppeteer as a sidecar executable, but increases the binary size a lot

See:

  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/package.json#L8-L12
  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/copy_node.js
  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/src-tauri/tauri.conf.json#L67-L73
  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/src/plugins/paged.js#L135-L153

mildred avatar Jul 22 '24 17:07 mildred

There is perhaps a way to make WebKitGtk print using the PDF print printer, but this virtual printer might not be programmable as well as the others.

mildred avatar Jul 22 '24 17:07 mildred

To perform a silent print, you can use plugin Tauri Plugin Printer. However, currently, it only supports Windows.

This plugin (tauri-plugin-printer) is just a hack it doesn't actually print the current webview instead it uses a library called html2pdf which is not that good unfortunately for many use cases.

Nothing beats the browser at rendering and producing high fidelity pdf files.

So this feature is really essential.

ohellel avatar Jul 22 '24 17:07 ohellel

For now I workaround this by including node and puppeteer as a sidecar executable, but increases the binary size a lot

See:

  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/package.json#L8-L12
  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/copy_node.js
  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/src-tauri/tauri.conf.json#L67-L73
  • https://github.com/mildred/web_docs/blob/74054afd143ab0c38b73300bf8c5eacaafcdec4a/src/plugins/paged.js#L135-L153

Just use electron instead of tauri+puppeteer, plus electron has multi window feature, child window, silent printing and a lot more.

But if tauri can catch up to it, it will be a better solution.

ohellel avatar Jul 22 '24 17:07 ohellel

I think I'll stop there at my attempts at PDF generation, I updated the PR to keep the ability to define options for printing, but I will find another way for generating PDF. Most probably I'll spawn a local web server and open the user browser to it and let the user use the system print dialog for PDF generation or real printing.

mildred avatar Jul 23 '24 22:07 mildred