How To Export PDF In Angular

Exporting an HTML element is like a snapshot for sharing some information in the form of documents, and it may be used for reporting too. So, if you need to save as a PDF or want to take a print-out of any other HTML elements like Table, Div or any Grid in Angular, you can do that easily.
 
Follow the steps for an example of how to do a  PDF Export.
 
Step 1
 
Create an Angular application using CLI or any other method you want.
 
Step 2
 
Open this application code in any editor like Visual Studio Code.
 
Step 3
 
For demo grid data, declare a variable with some sample data.
  1. Data = [  
  2.     { Id: 101, Name: 'Nitin', Salary: 1234 },  
  3.     { Id: 102, Name: 'Sonu', Salary: 1234 },  
  4.     { Id: 103, Name: 'Mohit', Salary: 1234 },  
  5.     { Id: 104, Name: 'Rahul', Salary: 1234 },  
  6.     { Id: 105, Name: 'Kunal', Salary: 1234 }  
  7.   ];  
Step 4
 
Bind that data with an HTML table or any element, this must how you want your PDF Document. In my app.component.html, I just replaced the default HTML with the following HTML code. In this code, I bind that data variable from my component to this table.
  1. <h1>This is an Angular App!</h1>    
  2. <div>    
  3.     <table>    
  4.         <tr>    
  5.             <th>Id</th>    
  6.             <th>Name</th>    
  7.             <th>Salary</th>    
  8.         </tr>    
  9.         <tr *ngFor="let item of Data">    
  10.             <th>{{item.Id}}</th>    
  11.             <th>{{item.Name}}</th>    
  12.             <th>{{item.Salary}}</th>    
  13.         </tr>    
  14.     </table>    
  15. </div>     
Now serve your Angular App in the browser using “ng serve --o”, this will be the default output.
 
How to Export PDF in Angular 
 
I know this is not the output that you want to download, so use the formatting and modify this as you need. In my Demo App, I’ll use bootstrap just for a simple table format.
 
Step 5
 
Add Bootstrap using CLI in your project and apply the “table table-dark” class on that HTML table. Use the following CLI command to add bootstrap in your project,
 
“npm install bootstrap –save”
 
Step 6
 
After adding bootstrap in your application, apply the required bootstrap classes like this,
  1. <div class="container">  
  2.     <table class="table table-dark">  
  3.         <tr>  
  4.             <th>Id</th>  
  5.             <th>Name</th>  
  6.             <th>Salary</th>  
  7.         </tr>  
  8.         <tr *ngFor="let item of Data">  
  9.             <th>{{item.Id}}</th>  
  10.             <th>{{item.Name}}</th>  
  11.             <th>{{item.Salary}}</th>  
  12.         </tr>  
  13.     </table>  
And now output must be like this,
 
How to Export PDF in Angular 
 
Step 7
 
Add a button to download this table as a pdf.
  1. <input type="button" value="Download PDF" class="btn btn-success" (click)="SavePDF()">  
Also, add a click event handler for this button in your app.component.ts like,
  1. public SavePDF():void{  
  2.   
  3.   }  
Step 8
 
Now Add jspdf and @types/jspdf in your application using the following command,
 
npm install jspdf --save”
 
and
 
npm install @types/jspdf --save-dev”
 
Step 9
 
Give a ref name and id to that HTML element you need to export as PDF and get the ref of that HTML element in your component using @ViewChild, like,
  1. <div class="container" id="content" #content>  
  2.   
  3.  @ViewChild('content') content:ElementRef;  
Step 10
 
Now at this last step, you have to write the following code in your button click handler function, where we create a jspdf variable as doc and configured it with some required permeates like Doc width, margin, and content,
  1. import * as jsPDF from 'jspdf';  
  2. public SavePDF(): void {  
  3.     let content=this.content.nativeElement;  
  4.     let doc = new jsPDF();  
  5.     let _elementHandlers =  
  6.     {  
  7.       '#editor':function(element,renderer){  
  8.         return true;  
  9.       }  
  10.     };  
  11.     doc.fromHTML(content.innerHTML,15,15,{  
  12.   
  13.       'width':190,  
  14.       'elementHandlers':_elementHandlers  
  15.     });  
  16.   
  17.     doc.save('test.pdf');  
  18.   }  
Now serve your application “ng serve --o” and your output must be like this,
 
How to Export PDF in Angular 
 
Click on the Download PDF button and get the file.
 
How to Export PDF in Angular 
Full Code
 
App.component.html
  1. <div class="container">  
  2.     <h1>This is an Angular App!</h1>  
  3.     <input type="button" value="Download PDF" class="btn btn-success" (click)="SavePDF()">  
  4.     <br/><br/>  
  5.     <div class="container" id="content" #content>  
  6.         <table class="table table-dark">  
  7.             <tr>  
  8.                 <th>Id</th>  
  9.                 <th>Name</th>  
  10.                 <th>Salary</th>  
  11.             </tr>  
  12.             <tr *ngFor="let item of Data">  
  13.                 <th>{{item.Id}}</th>  
  14.                 <th>{{item.Name}}</th>  
  15.                 <th>{{item.Salary}}</th>  
  16.             </tr>  
  17.         </table>  
  18.     </div>  
  19. </div>  
App.component.ts
  1. import { Component, ViewChild, ElementRef } from '@angular/core';  
  2. import * as jsPDF from 'jspdf';  
  3.   
  4. @Component({  
  5.   selector: 'app-root',  
  6.   templateUrl: './app.component.html',  
  7.   styleUrls: ['./app.component.css']  
  8. })  
  9.   
  10. export class AppComponent {  
  11.   title = 'app';  
  12.   Data = [  
  13.     { Id: 101, Name: 'Nitin', Salary: 1234 },  
  14.     { Id: 102, Name: 'Sonu', Salary: 1234 },  
  15.     { Id: 103, Name: 'Mohit', Salary: 1234 },  
  16.     { Id: 104, Name: 'Rahul', Salary: 1234 },  
  17.     { Id: 105, Name: 'Kunal', Salary: 1234 }  
  18.   ];  
  19.   
  20.   @ViewChild('content') content: ElementRef;  
  21.   public SavePDF(): void {  
  22.     let content=this.content.nativeElement;  
  23.     let doc = new jsPDF();  
  24.     let _elementHandlers =  
  25.     {  
  26.       '#editor':function(element,renderer){  
  27.         return true;  
  28.       }  
  29.     };  
  30.     doc.fromHTML(content.innerHTML,15,15,{  
  31.   
  32.       'width':190,  
  33.       'elementHandlers':_elementHandlers  
  34.     });  
  35.   
  36.     doc.save('test.pdf');  
  37.   }  
  38. }  
I hope it’ll work for you, keep sharing.