Printing a content of page through javascript


A lot of time as we know that we required make a print functionality to print a our web page as to print a specific portion or a whole page,
so below is the code to print a specific portion of a web page just need to client id of that portion (portion could be a table or a div any html container).

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.

strid=ClientId Of Portion (Div,Table etc)



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" />


Popular Posts