Server-Side Printing from ASP

Server-Side Printing from ASP

by John Peterson

Normally when I get questions relating to printing and ASP, the question is coming from a user that simply doesn't understand that ASP doesn't really apply if you're talking about printing from the browser (aka. client-side). That being said, every once in a blue moon, an email will roll in from a user that really does want to print from ASP, meaning that they actually want a printer connected to the web server to print something out.

The question that probably immediately comes to mind is: Why? In the paper-less world of the web, why would you ever want a web server to print something? Well in the case of sites like ASP 101, there's really no good reason. Our servers don't have printers connected to them, and, even if they did, we're hosted in a hosting facility so no one would ever know when something was printed. Heck, I've never ever seen the physical computers that host our site. I couldn't connect a printer to one even if I wanted to!

But here's the catch, not all web sites are like ours. Some companies actually host their sites in-house where printing might be useful. Suppose such a site processed orders over the web. Wouldn't it be cool if you could automatically print packing lists for the guys in the warehouse whenever an order came in or was approved? Intranets are another great example where printing from the server could be quite useful. Whether it's a list of user login sessions or an end of the day status report, simple little printouts can be quite useful.

Okay, So How Do I Print from ASP?

Easy, buy a component. That's the answer you'll hear most often if you ask how you can print from an ASP page. And to be honest, it's really not bad advice. If you need to print anything other then simple text, either buying or building a component to handle it is probably your only option. The script below doesn't handle graphics, PDF files, Word documents, XL sheets, or anything else. Plain text is all you get!

If you're willing to deal with the limitation of printing plain text, you're in the right place.

Note: If you had sent me an email about how to print from ASP six months ago, you wouldn't have been in the right place! I would've simply sent you to Microsoft's site to read an article called "Using ASP and WSH to Print on Your Intranet". What prompted me to actually write this article was that I recently tried to send someone to read that article and I couldn't find it. I searched the web and found a number of links to it, but the article itself appears to be gone. I believe that's where the concept I use below may have originally come from, but to be honest I don't really recall. While the article wasn't all that exciting, someone did spend the time to write it and it contained some useful information (including how to print to a network printer) and yet for some reason it was pulled from the site. If it was out of date, update it... I don't understand killing it altogether. As far as I can tell, the only thing it really accomplishes is to annoy the people who are looking for it. Sorry for the rant folks... now back to the code...

The Code

The code is actually very simple and should make perfect sense to anyone who ever spent any length of time at a command prompt. We use the FileSystemObject to create a new text file named LPTx: (which the system sends to the printer connected to LPT port x), we output the text we want printed, we end with a form feed to get the printer to spit the page out, and we close the file. That's it.




<%@ Language="VBScript" %>

<% Option Explicit %>

<html>

<head>

<title>ASP 101's Server-Side Printing Sample</title>

</head>

<body>



<%

Dim objFSO

Dim objPrinter



Set objFSO = Server.CreateObject("Scripting.FileSystemObject")



Set objPrinter = objFSO.CreateTextFile("LPT1:", True)



objPrinter.WriteLine "This is a test of printing from ASP!"

objPrinter.Write vbFormFeed



objPrinter.Close



Set objPrinter = Nothing



Set objFSO = Nothing

%>



</body>

</html>

It took me a few minutes to remember that I had to send a form feed character to get the printer to spit the page out, but aside from that it really couldn't be much simpler. The one other limitation that I did run into is that I can't seem to get it to print to a printer connected via USB. Also, if I recall correctly, printing to a network printer is more complex and I don't have that code handy, but if anyone wants it, I'll see if I can whip something up.

Note: For those of you looking for information about printing to a networked printer, please see Server-Side Printing to a Networked Printer from ASP.

Formatting

Assuming you can live with the limitation of only being able to print text, this approach is easiest way you're going to get ASP to print anything. The one real downside is all the string formatting you'll need to do to get your output to look pretty. Thankfully I've got a work-around for that: template files. I've included a sample in the zip file below. The basic idea is that you set up a template file with placeholders for the values you need to change at runtime. You then read in the template, plug in the values, and print out the results. It makes things a lot easier then trying to do all the formatting via VBScript and you won't go crazy looking at vbCrLf and vbTab.

Download

You can download a zip file containing the script above as well as the template sample from here: aspprint.zip (1.3 KB)

Close    To Top
  • Prev Article-Web Design:
  • Next Article-Web Design:
  • Now: Tutorial for Web and Software Design > Web Design > ASP > Web Design Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction