Solved

how to print invoices in asp

Posted on 2003-10-28
18
325 Views
Last Modified: 2010-04-17
i want to print the selected part of an asp page using code
while the rest of the page is diplayed

thanks in advance
0
Comment
Question by:Aeldin
18 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
The trick will be popup another formatted page where contains only the part you want to print. And in the body onload event of this popup page, try show the print dialog by adding window.print();
0
 

Accepted Solution

by:
gdean earned 20 total points
Comment Utility
That is one way of doing.  I prefer to use css and hide whatever I don't want printing.  There are a couple of ways of doing this.  The @media tag is supposed to allow you to define css rules for different media in one style sheet.  I however have found that some browsers, inlcuding IE5 leak the print styles into the screen styles.

I would recommend using a seperate css file for your print styles and just including the the below tag:

<link rel="stylesheet" media="print" href="INSERT YOUR URL" type="text/css">

You could try defining the styles in the asp page as well, however I believe this will still leak to the screen styles in some browsers.

The above tag is the safest way, IMO.

If your not familar with css, its a really simple concept/langauge:

Heres a link about the @media tag

http://www.w3schools.com/css/css_mediatypes.asp

Heres one with a ton of examples

http://www.w3schools.com/css/css_examples.asp
0
 
LVL 9

Expert Comment

by:bhagyesht
Comment Utility
Assuming you want to print the data between the table tag having id and name "table1"
the following will help you achive this.

<script>
var newwindow=window.open("about:blank");
newWindow.document.write (window.table1.innerHTML);
newWindow.print
newWindow.close
</script>

the table must be before this script tag.
0
 

Expert Comment

by:gdean
Comment Utility
If you want to do it this way Id recommend adding a link to the table below/above
such as:

<a href="javascript:sendPrint();">print</a>

then I'd encapsulate the printing code in the sendPrint function

function sendPrint()
{
var newwindow=window.open("about:blank");
newWindow.document.write (window.table1.innerHTML);
newWindow.print
newWindow.close
}

This way the table wont print everytime the page is loaded
0
 
LVL 9

Expert Comment

by:bhagyesht
Comment Utility
gdean: that depends on the asker maybe he wants to do it everytime or might be an asp page to print it specifically. yes you can do it like you said. I just guided the asker.
if its for netscape dont use window.table1 but getelementsbyname
0
 

Expert Comment

by:gdean
Comment Utility
use either getElementsByName() or getElementById()

asker: it is case sensitive

also change these lines

>>var newwindow=window.open("about:blank");
>>newWindow.print
>>newWindow.close

to

var newwindow=window.open('about:blank');
newWindow.print();
newWindow.close();

0
 
LVL 9

Expert Comment

by:bhagyesht
Comment Utility
gdean: yes you are right. i just wrote the code from memory. this shows signs of aging ;-) been working a lot on vbscript instead of javascript.
0
 

Expert Comment

by:gdean
Comment Utility
yeah, I can understand where you're coming from.  Its easy enough to do.
0
 

Expert Comment

by:gdean
Comment Utility
opps


you'll also need to close the document so you can print

function sendPrint()
{
var target = getElementById('table1') // <-- use whatever your table's id is
var newwindow=window.open('about:blank');
newWindow.document.write (target .innerHTML);
newWindow.document.close();
newWindow.print();
newWindow.close();
}

that should do it here is a link with someone that had a simalr question

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20702779.html
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:gdean
Comment Utility
opps

var target = getElementById('table1')

should of course be

var target = getElementById('table1');
0
 
LVL 9

Expert Comment

by:bhagyesht
Comment Utility
Aeldin: some feedback please?
0
 

Expert Comment

by:gdean
Comment Utility
>> that depends on the asker maybe he wants to do it everytime >> or might be an asp page to print it specifically.

true, however, its not a very good idea to have free code such as this executing while the page loads.

Consider, what would happen if the tbale contained an image, or later down the road it was modified to contain an image.  With the code floating, there is no way of knowing what might happen.  Even if it did work its not the right way to write javascript.

If you wanted the table to print everytime the page loads the code should still be wrapped in a function and called from the onLoad event.

here's an example of how this could be done.

<body onLoad="javascript:sendPrint();">

function sendPrint()
{
var target = getElementById('table1')
newwindow=window.open('about:blank');
newWindow.document.write (target .innerHTML);
newWindow.document.close();
newWindow.print();
newWindow.close();
}


0
 

Expert Comment

by:gdean
Comment Utility
I mean

var target = getElementById('table1');
0
 
LVL 9

Expert Comment

by:bhagyesht
Comment Utility
gdean: body's onload wont work as Aeldin wants to print while the page is loading 'maybe some heavy download or could be even an advt'.
Aeldin: Can we have some feedback?
0
 

Expert Comment

by:gdean
Comment Utility
ahh i see that now
0
 

Expert Comment

by:gdean
Comment Utility
Aeldin:

If this was your intention, printing while the page loads, its not a good idea.  You should be using the onLoad event to trigger the printing.

ARE YOU THERE?
0
 
LVL 9

Expert Comment

by:bhagyesht
Comment Utility
looks like Aeldin has done the vanishing act. so more job for the cleanup fellas
0
 

Author Comment

by:Aeldin
Comment Utility
Thanks for all , sorry for being late for replying because it's my first time to use this great website ,
thanks again for all
aeldin
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dividesSelf challange 15 77
sumHeights  challenge 17 59
countHi challenge 25 84
Microsoft C++ code failing in executable that worked 9 50
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now