jump to content

In many sites, you might need to have a printable version of HTML pages. Typically, this version lacks the design elements of the layout like navigation, top bar etc. An example can be seen in this site.

I use a PHP script for this purpose. It seems to be quite popular. So, I decided to write an ASP version of it.

To use it, save the code as an ASP file. From other pages, make a link to this ASP file. Click on that link and ASP script reads in the HTML, strips the layout elements and shows the stripped version.

In your HTML files, you MUST place your content within two comments like

<!-- content_starts_here //-->
<!-- content_ends_here //-->

Denis Mekinda adds: " If you try to use some non standard characters you will get garbage! The solution is to use UTF-8 encoding on all pages , since xml parser from MS parses in this format blindly! See responseText property in this MSDN article. ". Thanks Denis!

ASP Code

<%@LANGUAGE=VBScript%>
<%Option Explicit%>
<% Response.Buffer = True
   Response.CacheControl = "Private"
   Response.Expires = -1
'We don't want this to be cached anywhere
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' print.asp - Make a link to this page from your page.
' Assumes that your page has printable content within two comments
' content_starts_here and content_ends_here. Gets the page, strips of the
' the content, gets the BaseURL and prints the stripped out page
' Needs MSXML 3.0 or above installed
'
' S Babu. vsbabu-removethis@vsbabu.org. 05/23/2001
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Request.ServerVariables("HTTP_REFERER") <>  "" Then
  ' if this didn't come from anywhere, ignore.
  Dim strContent

  Dim xml_http
  Set xml_http = Server.CreateObject("Microsoft.XMLHTTP")
  xml_http.Open "GET", Request.ServerVariables("HTTP_REFERER"), False
  xml_http.Send
  strContent = xml_http.responseText
  Set xml_http = Nothing

  Dim regex
  Dim reg_matches
  Dim strtitle
  Dim strbody

  'On Error Resume Next
  ' Get the body
  Set regex = New RegExp
  regex.Pattern = "<!--\s+content_starts_here\s+\/\/-->(.|\n)*<!--\s+content_ends_here\s+\/\/-->"
  regex.IgnoreCase = True
  regex.Global = True
  strbody = strContent
  Set reg_matches = regex.Execute(strbody)
  If reg_matches.Count > 0 Then
    strbody = reg_matches.Item(0).Value
  End If
  Set regex = Nothing
  Set reg_matches = Nothing

  ' Get the title
  Set regex = New RegExp
  regex.Pattern = "<title>(.|\n)*<\/title>"
  regex.IgnoreCase = True
  regex.Global = True
  strtitle = strContent
  Set reg_matches = regex.Execute(strtitle)
  If reg_matches.Count > 0 Then
    strtitle = reg_matches.Item(0).Value
  End If
  Set regex = Nothing
  Set reg_matches = Nothing

  ' if we didn't get any different content, regex failed. So print
  ' out the existing content
  If strContent = strbody Then
    Response.Write strContent
  Else
    'Print the stripped version
    Response.Write "<html><head>" & strtitle & "</head><body>" & vbCrLf
    Response.Write "<base href=""" & GetBaseHref(Request.ServerVariables("HTTP_REFERER")) &""">" & vbCrLf
    Response.Write strbody
    Response.Write "<HR><center>" & Request.ServerVariables("HTTP_REFERER") & "</center></body></html>"
  End If
End If

'''Given a full URL, gets the base URL. Assumes that URLs to the
'''default document is atleast terminated by /
Function GetBaseHref(strURL)
  Dim arrParts, i
  arrParts = split(strURL,"/")
  GetBaseHref = ""
  For i = 0 To (UBound(arrParts)-1)
    GetBaseHref = GetBaseHref & arrParts(i) & "/"
  Next
End Function
%>