Solution 1 :

function PrintDiv() {
  var divToPrint = document.getElementById('divToPrint');
  var popupWin = window.open('', '_blank', 'width=300,height=300');
  popupWin.document.open();
  popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
  popupWin.document.close();
}
<div id="divToPrint" style="display:none;">
  <div style="width:200px;height:300px;background-color:teal;">HTML</div>
</div>
<div>
  <input type="button" value="print" onclick="PrintDiv();" />
</div>

Problem :

window.print() sometimes works but other times just silently fails to open the print window, and I couldn’t find a way to reproduce it.

By