Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I guarantee printing format?

Printing

How can I guarantee printing format?

by  link9  Posted    (Edited  )
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.

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

Ok, now for the javaScript:
Code:
<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:

Code:
<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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top