How to print a text (coupon) via javascript?

Asked

Viewed 4,850 times

5

I have a commercial web application written in ASP.NET MVC4 with c#, where I need to print a direct sale closing coupon to a non-tax printer (Diebold).

Is there any way to send, whether by JS, a direct document to the printer, without viewing print or confirm anything?

As if I mounted a string in JS, or received via AJAX and printed it!

  • 2

    Not

  • @bfavaretto if I can specify what exactly is not possible, I might adapt the requirement to make the idea viable! The problem is in printing via JS or not showing the print dialog?

  • 1

    The JS that runs in the browser is purposely limited, for security reasons. You can’t even print without the dialog box, or to communicate directly with the printer. As Renan says in the answer below, in the case of IE it may be possible to circumvent this, with the right security settings and probably the use of some Activex control or something like.

2 answers

3


If you want to skip the preview and confirmation screens, you can’t cross-browser. This is from the browser implementation. Maybe is possible with IE if you set up a printer by default - although I don’t know IE at all and can’t guarantee that this is even possible - but I guarantee that in Chrome you can’t because it will necessarily show at least the dialog where you choose whether to send to a device or save as document.

Editing: Chrome has a kiosk mode, See the response of Vitor Pinho. However, the quiósque mode has its problems there. This solution has stopped working from one version to another before, so if you want to use it, you may need to freeze the browser version in your POS.

Javascript itself is built with this in mind. To ask for a JS print, you call the method print of the object window. Thus:

window.print();

In very basic terms... This is a way for you to say pro browser: "account that the user has pressed CTRL+P in the window where this script is executed!" Henceforth the executed print code is browser and not your.

Other than that, it is considered good practice for you to have a style sheet (CSS) printing, which is distinct from the "main" sheet of the site. This print style sheet usually removes background colors and images, headers, footers etc. to make the content more suitable for printing. When you print, don’t call window.print(); in the contents window: open a new tab with the contents And the print style sheet, and on this new tab call window.print();. Good luck!

  • Got it, and would you have any alternative to that? As the demand for web applications has grown considerably.

  • @Pedroramon let me repeat the reply of bfavaretto: nay. If that’s such a strong requirement, maybe you need a app, not of an application ;)

  • Thank you @Renan.. if I write an application to carry out this operation, and install it in the client, know tell me if I can communicate my page with this application?

  • I don’t see why not.

2

You can do it in Chrome by starting the browser in Kiosk mode:

  1. Create a Chrome shortcut
  2. Right click on the shortcut and select the properties option
  3. In the "Target" field add the following command at the end (after the quotes) : --Kiosk-Printing
  4. Close all instances of Chrome and initialize an instance by the created shortcut

This way when you ask to print the Chrome print window will not open and the print will be sent to the default printer. This is even the limitation, because it can only be sent to the standard system printer and you can’t choose another.

  • This solution is correct and has my vote in favour, but there are some problems. This option lives having bugs (i.e.: stops working from one version to another, causes minutes lag in printing) etc. #YMMV.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.