ngx-print icon indicating copy to clipboard operation
ngx-print copied to clipboard

How to call ngx-print from a function

Open thelionleo opened this issue 3 years ago • 3 comments

I would like to call ngx-print from a function in .ts file

I have a button that calls the server, when data gathering is complete, then, print.

How do it do this?

thelionleo avatar Jun 05 '21 14:06 thelionleo

Use a template reference in HTML "#print". Add a click event on any other element and hide the button where the template reference is set => invisible button, so you can use any element you want to trigger the print, as only button works.

<div (click)="triggerPrint();">
    <span>Print</span>
  </div>
<button #print printTitle="report" printSectionId="content-section" ngxPrint hidden="true" [useExistingCss]="true"
 ></button>

In TS file use the ViewChild to add the template reference and use it in your triggerPrint method


@ViewChild("print") print!: ElementRef;

public triggerPrint(): void {   
      this.print.nativeElement.click();
  }

Skuriles avatar Jun 16 '21 07:06 Skuriles

Does that work for you @thelionleo ?

broem avatar Jun 28 '21 16:06 broem

This solution works flawlessly. It will also work when the the "content-section" is set to display: none.

belicekm avatar Aug 19 '21 08:08 belicekm