Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Angular 14 ngx-print Project to Print HTML to PDF Document With Custom CSS Classes in TypeScript

Posted on January 19, 2023

 

 

Welcome folks today in this blog post we will be printing certain sections of html document to pdf document with custom css classes using ngx-print library in Typescript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new angular project using the below command as shown below

 

 

ng new sampleapp

 

 

npm i ngx-print

 

 

And after that you need to see the below directory structure of the angular project as shown below

 

 

 

 

And then we need to go to app.module.ts file and copy paste the following code

 

 

app.module.ts

 

 

TypeScript
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 {}

 

 

As you can see we are importing the ngx-print-element module and then we are including it inside the imports array.

 

 

And then you need to go to app.component.html file and copy paste the following html code

 

 

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">&times;</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>

 

 

As you can see we have different html sections out there we have paragraphs and table and then we have the popup window where we allow the user to print various sections of the html to pdf document.

 

 

Now you need to go to app.component.ts file and copy paste the following code

 

 

app.component.ts

 

 

TypeScript
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; }']
  };
}

 

 

 

Recent Posts

  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Android Java Project to Download Random Image From Unsplash Using OkHttp & Picasso Library & Display it
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme