Printing

See also Printing an iframe

Open and write data to popup, print popup contents, close popup

 

Although there is not a simple command that allows the printing of part of a document there are a couple of methods that can be used.
You can use CSS by creating a seperate style sheet to hide any content that is not required for printing or you can use a script.
The basic idea is that because the print command prints the whole page you create a new page containing only the text you want to print and print that page.

The text to be printed is contained in a div

<DIV id="print_div1" style="border:1px solid #000000">
 

</DIV>

The id of the div is passed to the function.

<a href="#null" onclick="printContent('print_div1')">Click to print div 1</a>

On clicking the link the following sequence of events happens.

  1. A new window is opened
  2. The contents of the div are dynamically written to the new window
  3. Data is sent to the printer
  4. The new window is automatically closed (IE only)

Other areas of a page can be printed simply by defining another div and link

<DIV id="print_div2">
 

</DIV>

<a href="#null" onclick="printContent('print_div2')">Click to print div 2</a>

Just add the below script anywhere in your html document.

<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Print Page</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}
//-->
</script>