I’m following the way to print a specific div inside html page.

JS Function


function printContent(div_id)
{
var DocumentContainer = document.getElementById(div_id);
var html = '<html><head>'+
               '<link href="css/template.css" rel="stylesheet" type="text/css" />'+
               '</head><body style="background:#ffffff;">'+
               DocumentContainer.innerHTML+
               '</body></html>';

    var WindowObject = window.open("", "PrintWindow",
    "width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
    WindowObject.document.writeln(html);
    WindowObject.document.close();
    WindowObject.focus();
    WindowObject.print();
    WindowObject.close();
    document.getElementById('print_link').style.display='block';
}
HTML CODE
 <div id='ad_div'> <!-- we want to print only this div and here is the link --> <a href='javascript:printContent("ad_div")' id='print_link'><?php __('Print');?></a> </div>
<pre>

this work for me 🙂

Advertisements