Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a page in Angular 4 with TS

I have an web app while in some pages I need to make a printer to print the page but I have a side bar and the rest ist the page for a component, how it is possible to not print the sidebar only this component, I have used in TS this code.

print() {
window.print();
}

Html code starts from this one.

div class="container">
//Here I have all the HTML source

</div>
like image 467
Abedin.Zhuniqi Avatar asked May 28 '18 13:05

Abedin.Zhuniqi


Video Answer


1 Answers

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;
}
like image 57
Krishna Rathore Avatar answered Sep 24 '22 00:09

Krishna Rathore