Rao Ch
Rao Ch

Reputation: 21

How to implement printing in web application

In my desktop application, I developed text-based reporting and printing through a batch file because the printing is in bulk size and some times we have to restrict printing a single copy like Fixed Deposit Receipts etc. We are using lqdsi 5235 dot-matrix printers working file. Present desktop application process as follows:

The Batchfile is like this:

Name of the Batchfile: Dosprint.bat

Type %1 > prn

For Network printing: Name of the Batchfile: Netprint.bat

Type %1 > \\SharedComputer\SharedPrinterName

And in application I redirect the printing as follows

Public Function printFile(ByVal mFileNamePath As String) As Boolean
Shell(Application.StartupPath & "\Printer\dosprint.bat " & mFileNamePath, AppWinStyle.Hide)
      Return True
End Function

Printing value is very high and thousands of papers to be print some times. It is very safe and I can control No.of copies and everything like Fixed Deposit Receipts etc.
Help me if there any way to implement the same process in web application.

Upvotes: 2

Views: 4197

Answers (2)

Rao Ch
Rao Ch

Reputation: 21

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.

Upvotes: 0

Jorge Alvarado
Jorge Alvarado

Reputation: 2674

if you mean how to run your batch file from a web application, you can do something like this:

  System.Diagnostics.Process.Start(file.FullName) //where file is a FileInfo class

As long as the file is reachable by your web application (for instance located inside the bin folder) and

The account on which your app is running has sufficient permissions to execute the file.

UPDATE:

The proper way to handle printing scenarios is to create a page that renders the content in a simple and easy way to be printed. For example, using a simple table for tabulated data, use white color for most of the presentation to avoid spending print cartridges unnecesarily, pictures that matches your needs in specific sizes like A4 or letter, etc. Then you can call this function in the body tag:

 <body onload="window.print();">
    <!--content specially designed for proper printing -->
 </body>

Upvotes: 1

Related Questions