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

Asked

Viewed 19,768 times

5

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

I see many examples on the internet but everyone asks for print confirmation.

  • No browser dialog box you mean?

  • this even without this dialog that the browser shows

  • I find it difficult, you will have to find a way to send the document directly to the printer. I do not know if it is possible to do this with JS.

  • Voce knows other ways?

  • I don’t know if it’s possible to do this, this is almost the same thing that you want to take a picture with the user’s webcam without him noticing or without needing the authorization

  • Can you send the ok? command by java script? why it loading Popup and confirming ok! would be no problem.

  • Good evening, the dialog box serves to configure the print and select the device, so I don’t see why not have it. To be more direct in the Javascript API there is no such option, what you can do is create your own browser using Chromium or Webkitgtk and force the software to pick the first printer you find.

  • This action hurts basic security and privacy standards.. all approved browsers block by default unless the user removes the lock.

Show 3 more comments

2 answers

2

Javascript-only, is not possible. If it were, it would be a major security breach, imagine you visiting any site and suddenly your printer fires by printing multiple sheets without your permission?

Perhaps it is possible through some extension development, but this is subject for another question.

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

  • Typo about:config to enter the settings tab (click "I’ll be careful, I promise") and proceed.
  • Create a new boolean 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. So even a link like the below will work:

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

But as you can see anyway it is necessary the permission on the part of the user.


ps: If you have taken the above test, don’t forget to re-enter the Firefox preferences tab and change the key print.always_print_silent for false (or click on "restore the pattern").

  • 1

    "would be a major security breach, imagine you visiting any site and suddenly your printer fires by printing multiple sheets without your permission" Good observation

0

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

Reply in English


(Free Translation)

I couldn’t find a solution for other browsers. When I posted this question, IE was the top priority and fortunately I found one for it. If you have a solution for other browsers (firefox, safari, opera) please share here. Thank you.

<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>

Send the print without the print dialog being opened.


See the page of QUESTION, there are many answers, some really interesting that is worth testing, as for example this solution for Chrome, I don’t know if it really works.

  • 1

    Vb script ?? I am in the . net mvc project and using java script!! can do this??

  • @Hansmiller your project is using HTML or is pure Javascript?

  • HTML !! Java script sets the command to print!!

  • So you’re going to put this code the same way you put javascript, only you put this code at the beginning of the head of your HTML, before javascript, I think it overwrites the print function or something like that. You even have the tag script there to assist, which in this case is the same tag for javascript only you are specifying that it is VB in the parameter. @Hansmiller

  • 1

    @Hansmiller, although the response of Kadu is very good, we have to note that it makes use of Activex, feature that Microsoft abandoned in MS Edge, as for FF, Chrome and Opera, you can make an NPAPI plugin that will work in the old versions. need not even say that both Activex and NPAPI bring severe vunerabilities, then the safest option is to develop an app for chome and make use of the usb, serial, printerProvider, etc..

Browser other questions tagged

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