14

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>
TheCoderGuy
  • 771
  • 6
  • 24
  • 51
  • 1
    Possible duplicate of [How do I hide an element when printing a web page?](https://stackoverflow.com/questions/355313/how-do-i-hide-an-element-when-printing-a-web-page) – baao May 28 '18 at 13:49
  • 1
    @baao Are you kidding with me, if you think it is duplicate show me here the answer. – TheCoderGuy May 28 '18 at 13:50

4 Answers4

27

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();
15

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;
}
Krishna Rathore
  • 9,389
  • 5
  • 24
  • 48
  • 2
    Your code it is working to print, but when I try to print or cancel then the whole page it is not working ? – TheCoderGuy May 28 '18 at 14:11
  • Yeah, I have the same problem – Alekya Aug 20 '19 at 14:09
  • 1
    The print function only works once, when I try to click again the click function is not triggered. – Philip Mutua Oct 16 '19 at 17:48
  • I have the same problem, and when i console.log it throws this error : core.js:7187 ERROR TypeError: Cannot read property 'postMessage' of null – Emmon Nov 15 '19 at 06:44
  • 2
    Not perfect but after this code I call: window.location.reload(); and the page is at least alive again – Kibi Jun 09 '20 at 14:11
  • Hello, the code creates the page but the text inside the form is empty and also the whole page goes blank, has anyone experience and sort this issues? – Julio Perez Mar 10 '21 at 08:36
11

In the side bar component scss file or css file...you can hide it when you are printing using media queries:

@media print {
  :host {
    display: none;
  }
}
Ajay Reddy
  • 1,475
  • 1
  • 16
  • 20
0

There is no Direct window print function in typescript We need to create a new function with print() and inside that print function write javascript print function as shown below

<div click("print()")class="container">
</div>

in Ts File

print() {
window.print();
}
Yashwanth
  • 1
  • 1