Bypass Printdialog in IE9

后端 未结 2 504
攒了一身酷
攒了一身酷 2020-12-15 14:53

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

相关标签:
2条回答
  • 2020-12-15 15:00

    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.

    0 讨论(0)
  • 2020-12-15 15:08

    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

    0 讨论(0)
提交回复
热议问题