Thursday 8 May 2014

Printing a content of page through javascript

The concept behind is that to open a black web page in a new window(sized as you required) and write the whole content of a specific printable portion at that new page then fire java script built in print method for that window that open a browser print dialog.
 
function CallPrint(strid)
 {
      var prtContent = document.getElementById(strid);
      var WinPrint = window.open('', '', 'letf=100,top=100,width=600,height=600');
      WinPrint.document.write(prtContent.innerHTML);
      WinPrint.document.close();
      WinPrint.focus();
      WinPrint.print();
      //WinPrint.close()   
 }
 
 
To print the current whole page matter just fire window.print() javascript built in function on a button click or a link button click event.
 
<input type="button" onclick="CallPrint('divPrnt')" value="Print Portion" />
<input type="button" onclick="javascript:window.print()" value="Print Current Page" />