In this tutorial, We will learn how to convert HTML pages into pdf into angular 11. Angular 11 HTML to pdf example. how to make Angular 11 produce pdf from HTML.

This tutorial will use jspdf, html2canvas, and jspdf package to generate pdf files with HTML. any project Many times we find the requirement to convert HTML to pdf in angular content to save in image format or pdf format for sending feedback to email users multiple times. So, here I am going to explain about converting angular HTML into PDF content into PDF files in an angular application. It will also help you step by step how to create HTML in pdf angular 11 application.

Step 1: Create an Angular app using CLI

First of all, We Create an angular app with using following  command and move it into the project folder to convert HTML to pdf Angular:

ng new angular-to-pdf

Step 2: Install dependencies:

install the other dependency libraries:

npm install jspdf
npm install html2canvas

Step 3: update app.component.ts file

We need to update the ts file to perform the required task is a pdf js angular 11, So open the app.component.ts file and put the following code:

import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
import * as jspdf from 'jspdf';
import html2canvas from 'html2canvas';

    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
export class AppComponent {
    title = 'html-to-pdf-angular-application';
    public convetToPDF() {
        var data = document.getElementById('contentToConvert');
        html2canvas(data).then(canvas => {
            // Few necessary setting options
            var imgWidth = 208;
            var pageHeight = 295;
            var imgHeight = canvas.height * imgWidth / canvas.width;
            var heightLeft = imgHeight;

            const contentDataURL = canvas.toDataURL('image/png')
            let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF
            var position = 0;
            pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
  'new-file.pdf'); // Generated PDF

In the file above, we have included the jspdf library and html2canvas in our ts file for converting HTML to pdf angular. Below we have a way to create a click of an HTML file, we find the content of the id to convert it and convert it into a pdf file. We are learning to convert HTML page into PDF In Angular 11.

Step 4: Update app.component.html file

   <strong>How To Convert HTML Page To PDF In Angular 11? -</strong>
   <input type="button" value="Convert" (click)="convetToPDF()"/>
   <table id="contentToConvert">
            <td>A Company</td>
            <td>Robert look</td>
            <td>B Company</td>
            <td>C Company</td>
            <td>D Company</td>
            <td>E Company</td>
            <td>F Company</td>

In the above file, We have a dummy content in this form which will be converted into a pdf file and a button at the top on clicking which request is passing for pdf file then convert HTML page to pdf in angular 11.

Step 5: Run the app

After completing all the needed steps, Run the app to execute the below command:

ng serve --o

I hope it can help you...