Bypass Printdialog in IE9
Asked Answered
E

2

3

hy i am looking for a way to bypass the printdialog in IE 9. I now that there are some ways for ie 7/8 but they dont work for me at ie9

can someone please give me a hint?

greets markus

Editheditha answered 14/6, 2012 at 15:9 Comment(0)
P
11

The key combination for success here appears to be calling the right function in the onclick event (e.g. Print() rather than window.print() ), as well as having the proper security settings configured in IE9 (as well as any other version of IE).

However, it appears security settings may not be required to be configured if the page with print-dialog-bypass ActiveX control is being accessed via a trusted secure HTTPS connection (one with a trusted SSL cert, rather than a self-signed SSL cert).

It does not work at all if the page is accessed via local file path. Keep both of those in mind if you intend to target users whose browsers you cannot control, however if such a situation is indeed your case, you're probably best using another approach altogether, using technology such as Java or require users to install native OS software, such as the coupon printing websites employ.

In any case, with the appropriate security settings, IE9 should allow you to bypass the print dialog popup window with the following code:

<!DOCTYPE html>
<html>
<head>
    <title>Print Test</title>
    <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>
</head>
<body>
    <object id="WebBrowser1" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"> </object>
    <a href="#" onclick="Print()">Click Here to Print</a>
</body>
</html>

This exact code worked for me in IE7, IE8 and IE9. I haven't yet had a chance to IE10 yet, but it might work there too. If anyone with IE10 can test, do report back. For best results, remember to run it from a hosted source, preferably a trusted HTTPS source, rather than on your local machine.

Here are the settings I had to configure in IE9 to get the above code to work. Again, it only worked when the page was being served from the web. It worked with less nagging . If I tried to load the same HTML file directly from my local machine, it did NOT work, even with the same security settings configured.

Pink highlighting simply denotes that such settings as configured are insecure. Note: you can also choose 'prompt' which is more nagging, but considered somewhat secure.

IE9 Security Settings

Philous answered 27/9, 2012 at 16:7 Comment(3)
I'm not sure, but it looks like the <object> tag in the body is redundant given the document.write() in the vbscript. I took it out and it still works in IE8.Kulda
I am running the code snippet with IE 11 and I get that the Print() function is not defined when clicking the anchor in the body.Anglim
My IE crashes with this code and with all security settings set up as shown above.Bargainbasement
C
0

I really just wanted to leave a comment but since IE has revved up this should be included here. To have multi platform support I use this for javascript:

     function PrintWindow() {

        var isIE11 = !!navigator.userAgent.match(/Trident.*rv[ :]*11\./);
        if (navigator.appName == "Microsoft Internet Explorer" || isIE11== true) {

            var PrintCommand = '<OBJECT ID="PrintCommandObject" WIDTH=0 HEIGHT=0 ';
            PrintCommand += 'CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
            document.body.insertAdjacentHTML('beforeEnd', PrintCommand);
            PrintCommandObject.ExecWB(6, 2); PrintCommandObject.outerHTML = "";
            window.close();

        }

        else {

              window.print();
        }

      }

This checks for IE 11 now. It's a new way of defining the userAgent for Microsoft. There are other articles about this.

Chondroma answered 12/9, 2015 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.