Printing to a Client Printer from a Web App

Printing to a client printer from a web app

if you created an application that hosted the web browser control (rather than using a web browser directly) you could control the printing process much more directly.

(assuming using Windows + IE is possible)

here are some example articles that might help:

Configure a WebBrowser Control

Using the Web Browser Control in your C# Applications

and the ever changing MSDN docs for the WebBrowser class, which has the Print method.

From the print method remarks: "You can use this method to implement a Print button similar to the one in Internet Explorer. This method prints the current document without requiring further user input."

How i can print on client side without browser?

Yes, it is possible. If your objective is to print on client printer using printerjob and java graphics libary then you need to invoke servlet-applet communication.

Separate printing code/module in jar from your application and call it client side through applet. Applet will download your jar and will print at client location.

Other way, generate ticket in web-page using html/jsp, call windows.print using javascript. When user will click print then it will print on client location.

How to implement printing in web application

Thanks to Mr. SachinKumar K, for his article FILE HANDLING AT CLIENT SIDE USING JAVASCRIPT in WWW.CODEPROJECT.COM. In this article he mentioned that the website has to be added to the trusted site list so that the ActiveX object can be created and run.

To the dos print, I followed the below system to get dos based printing from my website on client systems dot-matrix printer.

  1. Configuration on client System: (Internet Explorer/browser configuration)

    Open Internet Explorer -->à Tools -->à Internet Options -->àSecurity (tab).

    add the SERVER URL in Trusted Sites

    Note: uncheck "Required server verification (https:) for all sites in this zone (if your website is not HTTPS) to allow addition the site.

    Next enable ActiveX controls and Plug-ins using Custom Level Tab on the same page

    //create a batch file ex: printme.bat. And type the following command in the batch file.

    Type %1 > prn

    //The Batch file contain only one command line as above. You may change prn keyword to LPT1 or shared printer like \system_name\printer

    //If required, Grant IIS_IUSRS, IUSR permission to the folder contains the printme.bat file to access by the browser.

  2. Web Page Tags definition and javascript implementation:

// use PRE tag. It stores raw data (ASCII) in un-formatted manner

<pre id="predata" runat="server" style="display:none;"></pre>

<asp:Button Text="Print Report" runat="server" ID="btnprint" Width="101px" CssClass="buttonstyle" BackColor="DarkSlateGray" ForeColor="Aqua" OnClientClick="dosprint()" />

<%-- JAVA SCRIPT FOR PRINTING --%>
<script>
function dosprint () {
var fso, tempfile,mdata;
var fname = { key: 'value' };
fso = new ActiveXObject("Scripting.FileSystemObject");
function CreateTempFile(fname) {
var tfolder, tfile, tname, fname, TemporaryFolder = 2;
tfolder = fso.GetSpecialFolder(TemporaryFolder);
tname = fso.GetTempName();
fname.key = tfolder + '\\' + tname;
tfile = tfolder.CreateTextFile(tname);
return (tfile);
}
tempfile = CreateTempFile(fname);
mdata = document.getElementById('<%= predata.ClientID %>').innerText;
tempfile.writeline(mdata);
tempfile.close();
objShell = new ActiveXObject("WScript.Shell");
comspec = objShell.ExpandEnvironmentStrings("%comspec%");
objExec = objShell.Exec('c:\\temp\\printme.bat ' + fname.key);
// give double back slash to get a back slash in path
}
</script>

In the above code the PRINTME.BAT batch file is exist on client systems c:\temp directory.

The above system is worked for me.
Thanks everybody and happy coding.

Can Web Applications detect local printers?

Bluntly, the answer is no. Server-side code can't talk with client-side resources in that manner.

A possible solution might be to embed some client-side component into your web app which does what you
want, such as a Java applet or Silverlight item. I don't know off the top of my head whether they're capable of printing, but it might be an option worth investigating.

However, I would suggest the best way to handle it (if your use-case allows), is to serve up something capable of printing via native browser methods. You are able to serve a stylesheet which applies to printing specifically using the media="print" attribute on your link element. If that remains too imprecise (you'd have no control over the browser's paper size, margins, headers/footers etc), perhaps another option might be to serve a print-specific file such as a PDF. Ultimately though, what you are asking is not possible and it is a case of finding another way to fulfil your requirements.



Related Topics



Leave a reply



Submit