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.
Data = [
{ Id: 101, Name: 'Nitin', Salary: 1234 },
{ Id: 102, Name: 'Sonu', Salary: 1234 },
{ Id: 103, Name: 'Mohit', Salary: 1234 },
{ Id: 104, Name: 'Rahul', Salary: 1234 },
{ Id: 105, Name: 'Kunal', Salary: 1234 }
];
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.
<h1>This is an Angular App!</h1>
<div>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Salary</th>
</tr>
<tr *ngFor="let item of Data">
<th>{{item.Id}}</th>
<th>{{item.Name}}</th>
<th>{{item.Salary}}</th>
</tr>
</table>
</div>
Now serve your Angular App in the browser using “ng serve --o”, this will be the default output.
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,
<div class="container">
<table class="table table-dark">
<tr>
<th>Id</th>
<th>Name</th>
<th>Salary</th>
</tr>
<tr *ngFor="let item of Data">
<th>{{item.Id}}</th>
<th>{{item.Name}}</th>
<th>{{item.Salary}}</th>
</tr>
</table>
And now output must be like this,



Jaiby BabyPosted Apr 19, 2022, 3:58 AM
How can i export cellTemplate values into pdf?
Nhã NhãPosted Mar 18, 2021, 4:53 AM
What is the version
Ahmed MobasherPosted Oct 20, 2020, 6:16 AM
How Can i make this library support UTF-8 to use arabic lang ??
sajith kumaraPosted Sep 1, 2020, 1:49 AM
I have error with Angular 9 . TypeError: doc.fromHTML is not a function in angular
Bibekananda PradhanPosted Apr 27, 2020, 6:33 AM
Hi, nice article . i tried but in my pdf i want to show image also which is stored blob storage. and blob storage image is not displaying in pdf where as in page its displaying. if i am using any google image url it work both in page and pdf only blob images are not showing in pdf. no error also in console.
Sarathlal SaseendranPosted Jan 27, 2020, 7:35 PM
Good one Sir. Thanks for sharing!
Sourav Kumar DasPosted Jan 25, 2020, 12:21 PM
Nice and useful article Sir on Export PDF of Angular.
Farhan AhmedPosted Jan 25, 2020, 10:59 AM
Nice and useful....