Print a table from an html page
48,758
Solution 1
You can use media types print (here is tips, how print html page using stylesheets).
You can realize that through popup window - in this window show only table and send it to printer.
Simple example
<script>
function printDiv() {
var divToPrint = document.getElementById('areaToPrint');
newWin = window.open("");
newWin.document.write(divToPrint.outerHTML);
newWin.print();
newWin.close();
}
</script>
Solution 2
You can give style display:none
to all unwanted portions of the page. In that way you can print only table.
for ex:
<style>
@media only print {
footer, header, .sidebar {
display:none;
}
}
</style>
Author by
user1263746
Updated on July 18, 2022Comments
-
user1263746 almost 2 years
I have a html page, from which a table needs to be sent to a printer. I am using window.print right now.. but that prints the whole page... while I need to print just the table. Any ideas?
-
user1263746 about 12 yearsThe example does not open a new window... does nothing. Any hints?
-
yAnTar about 12 years1. Do you have div with id areaToPrint ? 2. In previous code i had small syntax error - getEElement. Now i check example - all works.
-
user1263746 about 12 yearsouterHTML has known issue with firefox and chrome... but the issue is resolvable. Thanks!
-
Guillermo Gutiérrez about 11 yearsDon't forget to close the document after finishing writing:
newWin.document.close();
-
Guillermo Gutiérrez about 11 yearsVery creative way to do it! +1
-
LYKS almost 10 yearsI tried this but I need to include the link for the CSS stylesheet for me to get the desired print output. How do I do that?
-
yAnTar almost 10 yearsYou should add newWin.document.write('<link rel="stylesheet" type="text/css" href="path_to_your_css_file">')