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');
    }
}
Answer from GsMalhotra on Stack Overflow
🌐
npm
npmjs.com › package › jspdf
jspdf - npm
In Angular projects, externals can be defined using custom webpack builders. 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.
      » npm install jspdf
    
Published   Nov 19, 2025
Version   3.0.4
🌐
GitHub
github.com › alpitg › angular-jspdf
GitHub - alpitg/angular-jspdf: Angular 7 + jspdf + pdf download sample
Angular 7 + jspdf + pdf download sample. Contribute to alpitg/angular-jspdf development by creating an account on GitHub.
Author   alpitg
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.

🌐
GitHub
github.com › kumargandhi › angular-jspdf-example
GitHub - kumargandhi/angular-jspdf-example: Export PDF file using JSPDF library in Angular project.
Export PDF file using JSPDF library in Angular project. - kumargandhi/angular-jspdf-example
Author   kumargandhi
🌐
GitHub
github.com › parallax › jsPDF › issues › 2605
How can we use jsPDF in a web worker from Angular 8 app? · Issue #2605 · parallax/jsPDF
There is any way to use web workers for parallelism when generating multiple PDF pages in separate threads(in web workers) in an Angular applications ? Because when we try we received an error like 'Uncaught ReferenceError: window is not defined' ! Is this because of the limitation of web workers when using DOM elements? It look like with simple JavaScript jsPDF can be used in web worker : https://github.com/jstarcher/webworker_jspdf_example/tree/master/js How can we do the same in Angular with TypeScript ?
Published   Oct 23, 2019
Author   sorryb
🌐
jsPDF
artskydj.github.io › jsPDF › docs › index.html
jsPDF - GitHub Pages
The fontconverter will create a ... code for jsPDF. You just have to add this generated js-File to your project. You are then ready to go to use setFont-method in your code and write your UTF-8 encoded text. If you are using Webpack (including managed cli tools like angular-cli or ...
Find elsewhere
🌐
GitHub
github.com › alpitg › angular-jspdf › blob › master › README.md
angular-jspdf/README.md at master · alpitg/angular-jspdf
Angular 7 + jspdf + pdf download sample. Contribute to alpitg/angular-jspdf development by creating an account on GitHub.
Author   alpitg
🌐
GitHub
github.com › topics › angular-pdf-js
angular-pdf-js · GitHub Topics · GitHub
A very simple PDF viewer implementation in Angular 11 based on the PDFjs library from Mozilla. Its a great place to start for anybody who is looking to build on top of the most basic features like Canvas layer + Text layer rendering.
🌐
GitHub
github.com › parallax › jsPDF › issues › 2892
I cannot use jsPDF with Angular 10 · Issue #2892 · parallax/jsPDF
July 24, 2020 - I was trying to print my page using jspdf library version 2.1.0 with Angular version 10. I've also added a question in stackoverflow and here is the link https://stackoverflow.com/questions/63667889/i-cannot-use-jspdf-with-angular-10 Rep...
Published   Aug 31, 2020
🌐
jsPDF
artskydj.github.io › jsPDF › docs › jsPDF.html
jsPDF - Documentation - GitHub Pages
One property is prepopulated. It is the 'events' Object. Plugin authors can add topics, callbacks to this object. These will be reassigned to all new instances of jsPDF.
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');
    }
}
🌐
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 - We are able to create Angular project and install jspdf. We are able to create table using jspdf and export PDF file. We are able to set title, set font size.. etc for our PDF file. Checkout complete code on Github and enjoy the demo.
🌐
GitHub
gist.github.com › desmondcain › 39462bc23c7c25e11cda0fa0eb79045d
jsPDF Angular 1.x Content Formatting Service · GitHub
jsPDF Angular 1.x Content Formatting Service. GitHub Gist: instantly share code, notes, and snippets.
🌐
C# Corner
c-sharpcorner.com › article › html-to-pdf-using-jspdf-in-angular
HTML To PDF Using JSPDF In Angular
February 13, 2023 - In this article, you will learn about Html to Pdf using jspdf in angular.
🌐
GitHub
github.com › sutin1234 › angular-pdf
GitHub - sutin1234/angular-pdf: make angular with jsPdf
make angular with jsPdf. Contribute to sutin1234/angular-pdf development by creating an account on GitHub.
Author   sutin1234
🌐
Githubhelp
githubhelp.com › alpitg › angular-jspdf
The angular-jspdf from alpitg - GithubHelp
angular-jspdf,alpitg | angular 7 + jspdf + pdf download sample from githubhelp.