Last updated on May 13th, 2016 at 12:30 pm

Print a web page using Javascript

This tutorial will help you print a entire webpage or a part of it using JavaScript. It just uses document.getElementById tag to get the id from where it should start printing. The code will look like this.

<script>
function Print_Div() {
        var prtContent = document.getElementById('main');
        var WinOpen = window.open('', '', 'width=800,height=650,scrollbars=1,menuBar=1');
        var page =  prtContent.innerHTML;
        WinOpen.document.write(page);
        WinOpen.document.close();
        WinOpen.focus();
		WinOpen.print();
    }
</script>
<body>
Outside Main <P>
<button onclick="Print_Div()">Print</button>
<div id="main">
Testing Main Printing, Only this area of the webpage will get printed.
</div>
</body>

Very easy and simple approach.

Leave a Reply

Your email address will not be published. Required fields are marked *