|
|
| D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | Create a Print View for your Web Pages |
|
Create a Print View for your Web Pages I've had some requests to demonstrate how to display articles in a printer-friendly format, so I threw together this little article. In the process, I realized that my own articles don't offer this option, so I'll take this opportunity to add this functionality to my articles. This article assumes that you are already comfortable with include files and their use for page layout. If not, you can read more on this topic here. My implementation of a print view may be different from how others do it, but it's very simple, easy to implement, and easy to understand. First of all, let me describe how my pages are typically laid out. I have a main content chunk of each page that is unique, but the rest of the page is typically layout, and thus comes from functions located in include files. This makes it easy for me to make changes to layout that affect the entire site without having to open up more than one file. In the case of these articles, I have a file called articleformat.asp that holds my formatting functions, which include ArticleHeader() and ArticleFooter(). ArticleHeader() is responsible for writing my opening HTML tags, HEAD section, TITLE, and BODY. It also displays the top navigation. ArticleFooter displays the closing signature, right-hand column, and other footer tags. For a print view, I want to display just my content in such a way that it will print ok from most browsers, which means no big graphics and a relatively narrow column format. The easiest way to make this change is of course in the header and footer functions. The simplest way to get my include files to recognize that they're in "print view" is to have them check for a querystring. I *could* use a parameter to the functions, but then I would have to be sure that every page dealt with that parameter properly, and since I'm not using that parameter right now, it would also mean going into every existing article and changing it's function call to include the new parameter. Not my preference. So, to make this page appear in printer friendly format, I simply add ?print_view=true to the URL, like so: printview.asp?print_view=true.
In my include files, here is the code I've added:
'Show normal header if querystring doesn't have print_view=true...
<%
Else
'Print View
%>
<table width="600" bgcolor="#FFFFFF">
<tr>
<td align="center"><h2>Stevenator's ASP Resource, ASPAlliance.com</h2></td>
</tr>
<tr>
<td align="center"><div class="articletitle"><%=title%></div></td>
</tr>
<tr>
<td align="center">
<b>http://www.aspalliance.com<%=Request.ServerVariables("URL")%></b>
</td>
</tr>
<tr>
<td>
<%
End If
%>
In the article footer, I simply enclosed everything but the closing table, body, and html tags in an if statement that only executes when not in print view. Finally, I added a link to the navigation to open up the article in a new window in print view. The link reads:
<%
If Request.ServerVariables("QUERY_STRING") = "" Then
querystring = "?print_view=true"
Else
querystring = "?" & Request.ServerVariables("QUERY_STRING") & "&print_view=true"
End If
%>
<a href="<%=Request.ServerVariables("URL") & querystring %>"
target="_blank">Print View</a>
|
|
|
|
|
|
|
|