Saturday, July 10, 2021

Direct Print From ASP.NET to Client Printer

Direct Print From ASP.NET to Client Printer

In this article I will explain with an example, how to Direct Print from ASP.NET to Client Printer

IE9:

VBSCRIPT is much more convenient than creating an ActiveX on VB6 or C#/VB.NET:

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.

Direct Print From ASP.NET to Client Printer
Direct Print From ASP.NET to Client Printer

Chrome (v18+):

In chrome (v18+) we have the --kiosk --kiosk-printing switches. One can print automatically to default printer without print confirmation.

Mozilla FireFox:

The objective is to print directly from browser without the popup print dialog.
have to change browser settings.
In Firefox, type about:config in url box and 
add New Boolean,
by clicking anywhere and use this value - "print.always_print_silent"
and set it to "true".

<!DOCTYPE html>

<html lang="">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Title Page</title>

<!-- Bootstrap CSS -->

<link

rel="stylesheet"

href=

"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"

integrity=

"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhj

VME1fgjWPGmkzs7"

crossorigin="anonymous">

<!-- HTML5 Shim

and Respond.js IE8 support of HTML5 elements and media queries -->

<!-- WARNING:

Respond.js doesn't work if you view the page via file:// -->

<!--[if lt IE 9]>

<script

src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.min.js">

</script>

<script

src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js">

</script>

<![endif]-->

</head>

<body>

<h1 class="text-center">Print from HTML</h1>

<p>Hello world! This is HTML5 Boilerplate.</p>

<input type="submit" name="cetakbutton" id="cetakbutton"

value="Cetak" class='btn btn-primary'

onClick='javascript:hidePrintButton();'>

<!-- jQuery -->

<script src="//code.jquery.com/jquery.js"></script>

<!-- Bootstrap JavaScript -->

<script

src=

"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"

integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5E

Lqxss1yVqOtnepnHVP9aJ7xS"

crossorigin="anonymous"></script>

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->

<script src="Hello World"></script>

<script>

function hidePrintButton() {

// hide print button

document.getElementById('cetakbutton').style.visibility = 'hidden';

// now printing the

page - send to printer

self.print();

}

</script>

</body>

</html>



No comments:

Post a Comment

DotNet Latest Technologies

  I'm not sure exactly what you are looking for or how "recent" the technologies that you might wan to look into are, but I...