Angular Convert HTML to PDF File Example
Hi,
angular html to pdf, angular generate pdf from html, html to pdf angular, how to convert html to pdf in angular, convert html to pdf in angular, how to generate pdf from html in angular
we will use pdfmake, html-to-pdfmake and jspdf package for generate pdf file from html view in angular app. let's see simple example of how to generate pdf from html in angular.
you can easily convert html into pdf document in angular 7, angular 8, angular 9, angular 10, and angular 11 versions.
Preview:
Step 1: Create New App
You can easily create your angular app using bellow command:
ng new myNewApp
Step 2: Install Packages
Now in this step, we need to just install ngmodule/material-carousel and angular/material in our angular application. so let's add as like bellow:
npm install --save pdfmake
npm install html-to-pdfmake
npm install jspdf --save
Step 3: Update Ts File
here, we need to update ts file as like bellow:
src/app/app.component.ts
import { Component, ViewChild, ElementRef } from '@angular/core';
import jsPDF from 'jspdf';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import htmlToPdfmake from 'html-to-pdfmake';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'htmltopdf';
@ViewChild('pdfTable') pdfTable: ElementRef;
public downloadAsPDF() {
const doc = new jsPDF();
const pdfTable = this.pdfTable.nativeElement;
var html = htmlToPdfmake(pdfTable.innerHTML);
const documentDefinition = { content: html };
pdfMake.createPdf(documentDefinition).open();
}
}
Step 4: Update HTML File
here, we need to update html file as like bellow code:
src/app/app.component.html
<div class="container">
<div id="pdfTable" #pdfTable>
<h2>Angular HTML To PDF Generator Example - ItSolutionStuff.com</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Website</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hardik</td>
<td>Savani</td>
<td>itsolutionstuff.com</td>
</tr>
<tr>
<td>Vimal</td>
<td>Kashiyani</td>
<td>hdtuto.com</td>
</tr>
<tr>
<td>Harshad</td>
<td>Pathak</td>
<td>nicesnippets.com</td>
</tr>
</tbody>
</table>
</div>
<button class="btn btn-primary" (click)="downloadAsPDF()">Export To PDF</button>
</div>
Now you can run by bellow command:
ng serve
now you can check it.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular Material Carousel Slider Example
- Angular Slick Carousel/Slider Example
- Angular Bubble Chart Example Tutorial
- Angular Login with Google Account Example
- Angular Material Input Mask Date Example
- Angular 11 RxJS Observable Example
- How to Create Routing Module in Angular 11?
- Angular PDF Viewer Example
- Angular 11 File Upload Example
- Angular 11 CRUD Application Example
- How to Install And Use JQuery in Angular?