Server Side Printing with ASP

Kalimero used Ask the Experts™
Hello to all.

My question is simple :

Let us say I have a server, called WEBserver. It runs win2000, and I have a default printer installed. Now, the WEBserver, as it name suggests, can be accessed by clients, and it can run ASP scripts.
(So basically, If I go to http://WEBserver/1.asp, it runs the asp code in that file..).

Now my problem is this :
I want the WEBserver to print a certain file I need, each time I access this 1.asp file.
In other words, each time a certain client accesses 1.asp, I want the ASP which runs on the server to submit a printout of a certain text file to it's default printer.

Is it possible? And if yes, then how?

b.t.w : I am not talking about printing from the client side via a window.print() thingy.. or activeX controls on the client side..

b.t.w 2: Since the WEBserver sits in another room, I don't want to have to answer any type of prompt or things like that every time someone accesses 1.asp.

Any Ideas?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
will this work ??

I can not test it currently, because my Wscript version is too old to support the "sleep" method. And without the sleep it does not work, then the keystrokes come too quick.

Of course it is needed that IUSR_machinename has rights on the wscript.dll. Normally you would not want to give that, but in this case it is probably an intranet, so you can be a little less secure...

Dim sTextFile
sTextFile = "C:\autoexec.bat"
PrintTextFile sTextFile

Sub PrintTextFile(ByVal sFilePath)
    Dim oShell

    Set oShell = Server.Createobject("WScript.Shell")
    oShell.Run("notepad.exe " & sFilePath)
    oShell.Sleep 100
    oShell.Sleep 100
    Set oShell = Nothing

End Sub



This is interesting.. Let me try it out, if it works I'm gonna give you the points..

i think that the command is actually:
notepad.exe /p  %f
and that you don't have to pass ALT+F4.  Notepad should automatically close.  

However, I'm not sure that you can create a "windowed" process from IIS.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.


graver, do you know of another way of submitting something to the printer then?

well, you could try to use a cmd line call:
cmd.exe /c view <filename> > LPT1

another method would be to create an exe that watches a folder for file drops.  You could either leave it running or schedule it to run periodically.


How do I use a command line call from ASP?

set ws = server.createobject("WScript.Shell")
Shell.Run strcommand
Directly from Bill:

Runs a program in a new process.

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
WshShell object.
String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file.
Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.
Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).
The Run method returns an integer. The Run method starts a program running in a new Windows process. You can have your script wait for the program to finish execution before continuing. This allows you to run scripts and programs synchronously. Environment variables within the argument strCommand are automatically expanded. If a file type has been properly registered to a particular program, calling run on a file of that type executes the program. For example, if Word is installed on your computer system, calling Run on a *.doc file starts Word and loads the document. The following table lists the available settings for intWindowStyle.

intWindowStyle Description
0 Hides the window and activates another window.
1 Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 Activates the window and displays it as a minimized window.  
3 Activates the window and displays it as a maximized window.  
4 Displays a window in its most recent size and position. The active window remains active.
5 Activates the window and displays it in its current size and position.
6 Minimizes the specified window and activates the next top-level window in the Z order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window in its current state. The active window remains active.
9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.

Example 1
The following VBScript code opens a copy of the currently running script with Notepad.

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad " & WScript.ScriptFullName
The following VBScript code does the same thing, except it specifies the window type, waits for Notepad to be shut down by the user, and saves the error code returned from Notepad when it is shut down.

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)
Example 2
The following VBScript code opens a command window, changes to the path to C:\ , and executes the DIR command.

Dim oShell
Set oShell = WScript.CreateObject ("") "cmd /K CD C:\ & Dir"
Set oShell = Nothing


Let me try it too.. I'm quite busy right now so it might take   a while, but i'll reply as soon as I can.

I know the subject is now a bit old, but as I found help reading it I thought to contribute a little.

I think I have a passable way to emulate the sleep method:
define a lapse value (in seconds)
lapse = 3
and call the waiter !
'---------- sub waiter--------------------
sub waiter
x=60*eval(left(right(FormatDateTime(Now),5),2)) + eval(right(FormatDateTime(Now),2))
do while not y = x + lapse
     y=60*eval(left(right(FormatDateTime(Now),5),2)) + eval(right(FormatDateTime(Now),2))
end sub

Now my own problem: I tried this code on 2 machines: a NT4 SBS and a win2k pro
On the first, the target app launches but the sendkeys dont seem to register
on the second one (win2k) the target app doesn't even launch...
addtionnaly on the Nt4 machine I cant print because the machine says there is no default printer... The target app being iexplore it launches as default user wich has no real profile on the machine so, no printer...

Any idea welcome.

No comment has been added lately and it seems that this question have been abandoned. So it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question or invite a Moderator to close this question if there's no reply from you after seven days.

In the absence of responses, I will recommend the following:

Split points to graver & sybe


Just trying to help for the cleanup...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial