How to print a div (with browser compatibility)

1.Create a html page

2.Link print.js file

function printSelection(node) {

var content = node.innerHTML

var pwin = window.open(”, ‘print_content’, ‘width=700px,height=900px’);

pwin.document.open();

pwin.document.write(‘<html><body onload=”window.print()”><img src=”http://demo1.ntsplhosting.com/print-js/vedanta_logo.jpg” width=”171″ height=”80″>’ + content + ‘</body></html>’);

pwin.document.close();

setTimeout(function() { pwin.close(); }, 5000);

}

3.Add a div with id=’printtext’

4.write text with in div

5.close div

6.add a button

<input id=”Button1″ type=”button” value=”Print” onclick=”printSelection(document.getElementById(‘printtext’));return false” />

7.Your page like:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>Print Demo Create By Sameer Kumar Das</title>

<script src=”print.js” type=”text/javascript”></script>

</head>

<body>

<input id=”Button1″ type=”button” value=”Print” onclick=”printSelection(document.getElementById(‘printtext’));return false” />

<div id=’printtext’>

asdasdasd asd

</div>

</body>

</html>

Leave a Reply