1

I can open the print page with the following code but I want the print directly to the printer without displaying the Print Dialog. And I need directly 2 pages printing.

printDiv() {
    $(document).ready(() => {
        var win = window.open();
        var prtn = document.getElementById("divYazdir").innerHTML;
        win.document.open();
        win.document.write('<' + 'html' + '><head></head>' + '<' + 'body' + '>');
        win.document.head.innerHTML = document.head.innerHTML;
        win.document.body.innerHTML = prtn
        win.document.write('<' + '/body' + '><' + '/html' + '>');
        win.document.close();
        win.print();
        win.close();
    });
}
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • 1
    Not sure this has anything to do with reactjs, does it? – Davin Tryon Oct 24 '17 at 11:07
  • 1
    You can't. Security policy of browsers will block any attempt to do so. Can you imagine what would happen if you could. If you landed accidentally on a bogus site and then your printer started pumping out porn, etc. – Keith Oct 24 '17 at 11:08
  • same question is asked here: https://stackoverflow.com/questions/4292373/javascript-print-without-print-dialog-box – vinni Oct 24 '17 at 11:11
  • 1
    Possible duplicate of [javascript print without print dialog box](https://stackoverflow.com/questions/4292373/javascript-print-without-print-dialog-box) – vinni Oct 24 '17 at 11:14
  • The best option, but not trivial. And can't be done browser only. Is to create a server application, node.js would be good for this,. Get the server to print the page, of course this means the server needs access to the printer. The server then renders & prints the page, with requests from client over some sort of Rest API. – Keith Oct 24 '17 at 11:17
  • @Keith "the server needs access to the printer". Fine if both client and server are on the same LAN I guess, but completely impractical on the web in general, unless the printer is also accessible on the public internet?? – ADyson Oct 24 '17 at 13:20
  • @vinni the solution in that link only works in IE (because it's written in VBScript), no other browsers can use it. Not that great in this day and age when IE use is much less common, and so many non-windows devices exist which don't even have the possibility of using IE. Fine if this app is destined for a locked-down corporate environment but pretty hopeless in any other context. – ADyson Oct 24 '17 at 13:23
  • @ADyson `but completely impractical on the web in general,` It depends, it of course doesn't work for joe public accessing your website, this is an opt in system were client needs to install the server at there site. Our delivery company does something similiar for printing despatch labels. – Keith Oct 24 '17 at 13:27
  • @Keith ok but the question gives no indication that this kind of setup is being used, or that the application is going to be used within a specific organisatoin. – ADyson Oct 24 '17 at 13:30
  • @ADyson There is no indication either way. So what's your point? – Keith Oct 24 '17 at 13:36
  • @Keith just that it was worth clarifying that your suggestion doesn't work in all circumstances, given that the circumstances are unclear. – ADyson Oct 24 '17 at 14:19

0 Answers0