How to print a page in Angular 4 with TS
first of all assign an id to that component, then:
const printContent = document.getElementById("componentID");
const WindowPrt = window.open('', '', 'left=0,top=0,width=900,height=900,toolbar=0,scrollbars=0,status=0');
WindowPrt.document.write(printContent.innerHTML);
WindowPrt.document.close();
WindowPrt.focus();
WindowPrt.print();
WindowPrt.close();
You can try this solution.
html file
<div class="container" id="component1">
//Here I have all the HTML source
</div>
<div class="container" id="component2">
//Here I have all the HTML source
</div>
<button (click)="printComponent('component1')">Print</button>
ts file
printComponent(cmpName) {
let printContents = document.getElementById(cmpName).innerHTML;
let originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}