npm i ngx-print-element
Example
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<div class="container"> <h2 class="my-3">ngx-print-element</h2> <table id="demo" class="table table-bordered"> <tr> <th>No</th> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr class="print-none" style="background: greenyellow"><!-- No print --> <td>01</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>02</td> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>03</td> <td>AIS Playground</td> <td>Nakhon Pathom</td> <td>Thailand</td> </tr> <tr class="print-none" style="background: greenyellow"> <!-- No print --> <td>04</td> <td>FPT Software</td> <td>Cau Giay</td> <td>Vietnamese</td> </tr> </table> <!--------------------------> <!---- 1# The first way ----> <!--------------------------> <h5 class="my-3">1# The first way doesn't need configuration</h5> <!-- Component --> <ngx-print-element #element="element"> <button (click)="element?.print('demo')" class="btn btn-info m-1">1# Print table</button> </ngx-print-element> <!-- Directive --> <button [print]="['demo']" class="btn btn-info m-1">Directive - 1# Print table</button> <!-- Service --> <button (click)="print?.print('demo')" class="btn btn-info m-1">Service - 1# Print table</button> <!--------------------------> <!--- 2# The second way ----> <!--------------------------> <h5 class="my-3">2# The second way needs configuration</h5> <button [print]="['demo', config]" class="btn btn-info">2# Print table</button> <!--------------------------> <!-- Button trigger modal --> <!--------------------------> <h5 class="my-3">3# Test modal</h5> <button type="button" class="btn btn-info" data-toggle="modal" data-target="#modal"> Open modal </button> <!-- Modal --> <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <p>ngx-print-element</p> <p class="print-none">HELLO WORLD</p><!-- No print --> </div> <div class="modal-footer"> <button type="button" [print]="['modal']" class="btn btn-info">1# Print modal</button> <button type="button" [print]="['modal', config]" class="btn btn-info">2# Print modal</button> </div> </div> </div> </div> <h5 class="mt-3">Author: DaiDH</h5> </div> |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import { Component } from '@angular/core'; import { NgxPrintElementService } from 'ngx-print-element'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { constructor(public print: NgxPrintElementService) {} public config = { printMode: 'template-popup', popupProperties: 'toolbar=yes,scrollbars=yes,resizable=yes,top=0,left=0,fullscreen=yes', pageTitle: 'Hello World', // templateString: '<header>I\'m part of the template header</header>{{printBody}}<footer>I\'m part of the template footer</footer>', stylesheets: [{ rel: 'stylesheet', href: 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css' }], styles: ['.table { color: red; }', '.table td { color: green; }'] }; } |
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { NgxPrintElementModule } from 'ngx-print-element'; @NgModule({ imports: [ BrowserModule, NgxPrintElementModule, ], declarations: [AppComponent], bootstrap: [AppComponent], }) export class AppModule {} |