🌐
npm
npmjs.com › package › jspdf
jspdf - npm
In React (create-react-app) projects, externals can be defined by either using react-app-rewired or ejecting. jsPDF can be imported just like any other 3rd party library. This works with all major toolkits and frameworks.
      » npm install jspdf
    
Published   Nov 19, 2025
Version   3.0.4
🌐
C# Corner
c-sharpcorner.com › article › html-to-pdf-using-jspdf-in-angular
HTML To PDF Using JSPDF In Angular
February 13, 2023 - This jsPDF plugin adds the ability to generate PDF tables either by parsing HTML tables or by using Javascript data directly. ... For this article, I have created an Angular project using Angular 12.
🌐
Nutrient
nutrient.io › blog › sdk › how to generate pdfs using angular
Generating PDFs in Angular with jsPDF and Nutrient Web SDK
July 16, 2025 - Here, the code defines an Angular component named PdfGeneratorComponent that generates a PDF document with text and a table when a button is clicked. It uses the jsPDF library for PDF creation and jspdf-autotable for building tables within the PDF.
🌐
StackBlitz
stackblitz.com › edit › angular-ivy-yfvdbk
Angular 12 Jspdf.html - StackBlitz
A angular-cli project based on rxjs, jspdf, tslib, jquery, zone.js, @angular/core, @angular/forms, @angular/common, @angular/router, @angular/compiler, @angular/animations, @angular/platform-browser and @angular/platform-browser-dynamic.
🌐
YouTube
youtube.com › coding diksha
Angular 12 HTML to PDF using HTML2PDF.js Library(jsPDF & HTML2Canvas Library) - YouTube
In this tutorial, I am going to teach you “How to Convert HTML Table to PDF using HTML2PDF.js in Angular 12 application”. https://codingdiksha.com/convert-ht...
Published   October 15, 2021
Views   4K
🌐
Eduforbetterment
eduforbetterment.com › good news › faith
Export or Open PDF in Angular using JSPDF - Education For Betterment
August 26, 2022 - Below is the full embeded code export or Open PDF in Angular using JSPDF. you can easily used in your application.
Find elsewhere
Top answer
1 of 5
84

I have done it, after doing lot of R&D , their are few steps to follow as below : Install :

npm install jspdf --save

typings install dt~jspdf --global --save

npm install @types/jspdf --save

Add following in angular-cli.json:

"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]

html:

<button (click)="download()">download </button>

component ts:

import { Component, OnInit, Inject } from '@angular/core';
import * as jsPDF from 'jspdf'
@Component({
  ...
  providers: [
    { provide: 'Window',  useValue: window }
  ]
})
export class GenratePdfComponent implements OnInit {

  constructor(
    @Inject('Window') private window: Window,
    ) { }

  download() {

        var doc = new jsPDF();
        doc.text(20, 20, 'Hello world!');
        doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
        doc.addPage();
        doc.text(20, 20, 'Do you like that?');

        // Save the PDF
        doc.save('Test.pdf');
    }
}
2 of 5
11

Old question but this is an alternative solution that I got up and running in my angular app. I ended up modifying this jsPDF solution to work without using the ionic/cordova CLI.

npm install jspdf --save
npm install @types/jspdf --save
npm install html2canvas --save
npm install @types/html2canvas --save

Add an id to whichever div contains the content you want to generate the PDF

<div id="html2Pdf">your content here</div>

Import the libraries

import * as jsPDF from 'jspdf';
import * as html2canvas from 'html2canvas';

Add the method for generating the PDF

generatePdf() {
    const div = document.getElementById("html2Pdf");
    const options = {background: "white", height: div.clientHeight, width: div.clientWidth};

    html2canvas(div, options).then((canvas) => {
        //Initialize JSPDF
        let doc = new jsPDF("p", "mm", "a4");
        //Converting canvas to Image
        let imgData = canvas.toDataURL("image/PNG");
        //Add image Canvas to PDF
        doc.addImage(imgData, 'PNG', 20, 20);

        let pdfOutput = doc.output();
        // using ArrayBuffer will allow you to put image inside PDF
        let buffer = new ArrayBuffer(pdfOutput.length);
        let array = new Uint8Array(buffer);
        for (let i = 0; i < pdfOutput.length; i++) {
            array[i] = pdfOutput.charCodeAt(i);
        }

        //Name of pdf
        const fileName = "example.pdf";

        // Make file
        doc.save(fileName);

    });
}

I found this solution worked well for my web app and was beneficial as I have control over when I want to generate the PDF (after receiving data asynchronously). As well, I didn't need install any libraries globally.

🌐
StackBlitz
stackblitz.com › edit › angular-12-jspdf-2
Angular 12 Jspdf 2 - StackBlitz
Starter project for Angular apps that exports to the Angular CLI
Top answer
1 of 6
22

What I found worked was adding:

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>

to the index.html file (it could presumably be elsewhere).

I then used:

const elementToPrint = document.getElementById('foo'); //The html element to become a pdf
const pdf = new jsPDF('p', 'pt', 'a4');
pdf.addHTML(elementToPrint, () => {
    doc.save('web.pdf');
});

Which no longer uses html2canvas in the code.
You can then remove the following import:

import * as html2canvas from 'html2canvas';
2 of 6
9

In case someone prefer not to use cdn scripts & would like to use a more (angular) way, this worked for me in Angular 6:

Using this way will give you better support & autocomplete in the editor & will help you avoid depending on cdn scripts (if you wanna avoid them, like me)

Based on the excellent answer here & since it was hard for me to find that answer, I am re-sharing what was stated in it & helped me use jsPDF in Angular 6 (all credit goes to the original author of this answer)

You should run these cmds:

npm install jspdf --save

typings install dt~jspdf --global --save

npm install @types/jspdf --save

Add following in angular-cli.json:

"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]

html:

<button (click)="download()">download </button>

component ts:

import { Component, OnInit, Inject } from '@angular/core';
import * as jsPDF from 'jspdf'
@Component({
  ...
  providers: [
    { provide: 'Window',  useValue: window }
  ]
})
export class GenratePdfComponent implements OnInit {

  constructor(
    @Inject('Window') private window: Window,
    ) { }

  download() {

        var doc = new jsPDF();
        doc.text(20, 20, 'Hello world!');
        doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
        doc.addPage();
        doc.text(20, 20, 'Do you like that?');

        // Save the PDF
        doc.save('Test.pdf');
    }
}
🌐
Stack Overflow
stackoverflow.com › questions › 70851364 › jspdf-not-working-html-function-with-angular-12-in-google-chrome
jsPDF not working .html function with Angular 12 in Google Chrome - Stack Overflow
The jsPDF (version 2.5.0) .html() function just loading and can't download the given HTML table in Angular 12 with Google Chrome - but in Edge works. Here is the Ang 12 sample stackblitz demo I fou...
🌐
Freakyjolly
freakyjolly.com › how-to-export-pdf-from-html-in-angular-12
How to Export PDF from HTML in Angular 12
April 30, 2023 - Execute below CLI command to create a new angular application. ... Next, install few library packages required to convert the HTML content into PDF files. Head towards the terminal window set to the application root, and execute below npm command ...
🌐
GitHub
github.com › parallax › jsPDF › issues › 3715
Unable to Render HTML from External File into PDF using Angular 12. · Issue #3715 · parallax/jsPDF
February 1, 2024 - I'm encountering difficulty rendering HTML content from an external file into a PDF within an Angular 12 application. ... viewPDF() { html2canvas(this.htmlFilePath).then((canvas) => { const imgData = canvas.toDataURL('image/jpeg'); const imgWidth = 210; const pageHeight = 297; const imgHeight = canvas.height * imgWidth / canvas.width; let heightLeft = imgHeight; const pdf = new jsPDF('p', 'mm', "A4"); let position = 0;
Published   Mar 18, 2024
🌐
Kumar Gandhi Koppolu
kumargandhi.in › 2021 › 10 › 28 › export-pdf-in-angular-using-jspdf
Export PDF in Angular with JSPDF. – Kumar Gandhi Koppolu
September 27, 2022 - Posted byKumar Gandhi KOctober 28, 2021September 27, 2022Posted inAngular, Bootstrap, JSPDF, ng new, pdf, Tips and Tricks, TutorialTags:Angular, Bootstrap, JSPDF, pdf, Tutorial · In this post we are going to create an Angular 12 application and export a PDF file using jspdf library.
Top answer
1 of 4
20

After trying so many different solutions for this issue now I've found that both the addHTML and fromHTML is depreciated in latest versions of jspdf and they are replaced with just html. For those who are seeking for a better solution to work with jspdf and Angular 10 here is what looks working and a better result that I've found so far.

To import jspdf version (2.x.x), just simply do: import { jsPDF } from 'jspdf';.

You won't need to include any of the dependencies into angular.json scripts like you would do in previous versions. And if you add scripts of jspdf or html2canvas insideindex.html, also remove them. By running npm install jspdf it also install the html2canvas which is also dynamically imported by jspdf.

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

.....

 @ViewChild('couponPage', { static: true }) couponPage: ElementRef;

.......

openPDF(): void {
  const DATA = this.couponPage.nativeElement;
  const doc: jsPDF = new jsPDF("p", "mm", "a4");
  doc.html(DATA, {
     callback: (doc) => {
       doc.output("dataurlnewwindow");
     }
  });
}

Though for me still using doc.html() method, it wouldn't use the css style that I used for my page.

2 of 4
3

After working with the OP for a short time on a stackblitz, it seems the main problem was that the usage of the library changed between versions 1.3.5 (the one used in the examples) to 2.1.0 (the one he was using). I am adding this answer so that it may provide a clue to future solution seekers who suffer from a similar problem.

🌐
DEV Community
dev.to › vidyarathna › generating-pdfs-in-angular-using-jspdf-3a6
Generating PDFs in Angular using jsPDF - DEV Community
May 29, 2024 - Integrating jsPDF into your Angular project allows you to create PDF documents dynamically within your application. By following the steps outlined above, you can set up jsPDF, create a service for PDF generation, and trigger this functionality ...