How to print a page directly, without a browser dialog, using Javascript?

How to print, that is, send a page directly to physical printing, without displaying the browser dialog before?

I see a lot of examples on the internet but they all ask for print confirmation.

Author: mutlei, 2015-03-18

2 answers

Only with Javascript, is not possible . If it were, it would be a major security flaw, imagine you visiting any website and out of nowhere your printer fires printing multiple sheets without your permission?

It may be possible through development some extension, but this is subject to another question.

At the knowledge level, in Firefox you can make some modifications that allow you to print a document without the prompt of confirmation as follows:

  • Type about:config to enter the Settings tab (click "I'll be careful, I promise") and proceed.
  • create a new boolean type preference. Name print.always_print_silent and mark it as true.

When restarting the browser, any call via Javascript with code window.print() will start printing automatically. Thus, even a link like the one below will work:

<a href='javascript:window.print()'>Imprimir essa página</a>

But as you can see anyway it is required permission from the user.


ps: if you have done the above test, do not forget to re-enter the Firefox Preferences tab and change the key print.always_print_silent to false (or click "Restore Default").

 2
Author: Renan Gomes, 2015-04-17 22:12:24

Found a" solution " in the OS.en, as I have no printer at home has no way to test, but feel free to test and tell us if it worked.

Answer in English


(Free Translation)

I found no solution for other browsers. When I posted this question, IE was the highest priority and luckily I found one for it. If you have a solution for other browsers (firefox, safari, opera) please SHARE it here. Thanks.

<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

Now call:

<a href="javascript:window.print();">Print</a>

Will send the print without the print window being opened.


Refer to the Question, there have many answer, some really interesting that is worth testing, as for example this solution for Chrome , which I do not know if it really works.

 0
Author: KaduAmaral, 2020-06-11 14:45:34