Convert HTML to PDF using jsPDF

Converting HTML to PDF is a common task in many web applications, and Angular developers have several options for implementing this functionality.

One popular library for converting HTML to PDF in Angular is jsPDF.

With jsPDF, you can generate a PDF document from HTML content and then save it to the user’s local file system or display it in the browser window.

In this article, we will walk through the process of using jsPDF to convert HTML to PDF in an Angular application, including installing the library, importing it into a component, creating a PDF document, and saving or displaying the resulting PDF file.

With the help of jsPDF, you can easily add PDF generation capabilities to your Angular application and enhance its functionality for users.

Here are the general steps to follow:

1) Install jsPDF library using npm:

npm install jspdf --save

2) Import jsPDF library in the component where you want to convert HTML to PDF:

import * as jsPDF from 'jspdf';

3) Create a function that generates a PDF from the HTML content using jsPDF:

public downloadPDF() {
  const doc = new jsPDF();
  const specialElementHandlers = {
    '#editor': function (element, renderer) {
      return true;
  const content = this.htmlContent.nativeElement;
  doc.fromHTML(content.innerHTML, 15, 15, {
    'width': 190,
    'elementHandlers': specialElementHandlers

In this example, the downloadPDF() function creates a new jsPDF instance and sets up special element handlers to include all elements in the HTML content. It then uses the fromHTML() method to convert the HTML content to PDF format and saves the resulting PDF file using the save() method.

4) Add an HTML button or link that triggers the downloadPDF() function when clicked:

<button (click)="downloadPDF()">Download PDF</button>

This creates a button that calls the downloadPDF() function when clicked, allowing users to generate and download the PDF file.

and that’s it, you can convert html to pdf using above code and can download pdf.

I hope you like this amazing article.

Also Read