INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Printing

How can I guarantee printing format? by link9
Posted: 18 May 01 (Edited 7 Mar 02)

This is a question that **haunts** me at work.  Some of what I do is online reporting, and these reports have to be printable, and printing from a browser has always proven to be a hit or miss proposition.

The good news?  If your users are willing to view your page in IE, and have Excel installed on their computers, then you are in business.  

The process I'm about to describe is fairly simple, and here's how it works --

You render some content to the browser, and everything that you want to print, you put into one table.  Feel free to nest tables inside that big table, but the key is to keep all the printable content inside the one parent table.  Once that's done, then all you have to do is give that parent table and name and id, add a very simple javaScript function to the page that will copy the contents of that table, and then open up a new window containing your pre-formatted Excel spreadsheet will all your print margins, headers, footers, etc... already built in.

Here we go -- this is a very simplified version, but can be scaled on up to however big you wish the table to be.


<table name=listTable id=listTable>
  <tr>
    <td>This will be what gets printed</td>
  </tr>
</table>


Ok, now for the javaScript:

<script language="javascript">
function fnDoCopy(){
 textRange = document.body.createTextRange();
 textRange.moveToElementText(listTable);
 textRange.execCommand("Copy");
 window.open("outputFile.xls","printing");
}
</script>


Notice that I put for the url in the window.open() statement, 'outputFile.xls'.  That is the name of your pre-formatted Excel file that contains all of your formatting.  I'll get to how to set that up in a second.

Now, you need to add a special 'print' button to your page that will look more or less like this:


<input type=button value=Print onClick="fnDoCopy();">


So that when someone clicks that button, the javascript function first copies the entire contents of your 'listTable' table to the clipboard, and then opens a new window that contains the Excel workbook.  All that's left for your users to do at that point is press [CTRL]-V and voila -- one beautiful printable page that you have complete control over.

Let me take just a second for the sake of completeness and tell you that in order to set up the Excel file in the first place, just save an Excel file as the pre-determined name and place it in your web directory.  The first time it's used here, it will be just a plain old file.  Then, visit your page, and press the print button.  That will open up your file for you.

Once it's open, paste in the contents of your clipboard, and start changing column widths, row heights, print margins, headers, footers, etc...  

Print it off and see how it looks.  Continue this process until it's perfect.  Once you have it perfect, save it (with the content).  Now that you have that, go in and delete all the content from the workbook, including any table borders, merged cells, background coloring, etc...  All that type of formatting will come in with each new pasting of content.

So now what you have is an Excel sheet with all your printing options set up to the T... nothing more, nothing less.  Resave the file under the same name.

Your file is now ready for use by your users, and the headaches of browser printing is a thing of the past.

Happy Coding!  
Paul Prewett

This method was actually proposed to me by member RobSchultz a while ago in the vbScript forum, and I have been refining it for a while.  Thanks, RobSchultz.  This method has saved me ALOT of headaches.

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close