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';
Answer from Greg on Stack Overflow
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');
    }
}
Discussions

how to set top and bottom margin in .HTML using jspdf
Hi, how did you solve this? More on reddit.com
🌐 r/Angular2
1
1
December 17, 2020
anyone experience with jsPDF? My image is cut off
Set the 5th and 6th parameters of your addImage to be the exact width and height of the image in the units you specified at the start of your code. It should be "pt", "mm", "cm", "m", "in" or "px". It defaults to mm, so assume you're giving measurements in mm if you haven't defined the unit. If there's no luck with that I'd also try omitting the width/height. Try this and see what happens: pdf.addImage(img, 'PNG', 0, 0); More on reddit.com
🌐 r/learnjavascript
3
1
August 12, 2019
Can I access the HTML of a component that is simply created in a TS file and not included in the actual app?
Can you provide some code? More on reddit.com
🌐 r/angular
5
7
February 18, 2016
What are you using to export tables to .pdf, .csv?
jsPDF creates PDFs in Javascript. I am using it, its working fine! More on reddit.com
🌐 r/angularjs
10
21
December 17, 2014
🌐
Nutrient
nutrient.io › blog › sdk › how to generate pdfs using angular
Generating PDFs in Angular with jsPDF and Nutrient Web SDK
July 16, 2025 - To generate PDFs in Angular, you’ll use two libraries: jspdf(opens in a new tab) and jspdf-autotable(opens in a new tab). Install them using npm: ... These packages will enable you to create and customize PDFs within your Angular application.
🌐
C# Corner
c-sharpcorner.com › article › html-to-pdf-using-jspdf-in-angular
HTML To PDF Using JSPDF In Angular
February 13, 2023 - import { Component, OnInit } from "@angular/core"; import jsPDF from "jspdf"; import "jspdf-autotable"; @Component({ selector: "my-app", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent implements OnInit { ngOnInit() {} getBase64Image(img) { var canvas = document.createElement("canvas"); console.log("image"); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); var dataURL = canvas.toDataURL("image/png"); return dataURL; } download() { let doc = new jsPDF(); doc.autoTable({html: '#table'}); doc.output('datauri','test.pdf'); } } ... In this article, I have discussed about implementation of JSPDF in Angular.
🌐
DEV Community
dev.to › vidyarathna › generating-pdfs-in-angular-using-jspdf-3a6
Generating PDFs in Angular using jsPDF - DEV Community
May 29, 2024 - import { Injectable } from '@angular/core'; import { jsPDF } from 'jspdf'; @Injectable({ providedIn: 'root' }) export class PdfService { constructor() { } generatePdf() { const doc = new jsPDF(); doc.text('Hello world!', 10, 10); doc.save('sample.pdf'); } } Next, generate a new component where you will add a button to trigger the PDF generation: ... In pdf-generator.component.ts, inject the PDF service and create a method to call the generatePdf function: import { Component } from '@angular/core'; import { PdfService } from '../pdf.service'; @Component({ selector: 'app-pdf-generator', templateUrl: './pdf-generator.component.html', styleUrls: ['./pdf-generator.component.css'] }) export class PdfGeneratorComponent { constructor(private pdfService: PdfService) { } generatePdf() { this.pdfService.generatePdf(); } }
🌐
Kumar Gandhi Koppolu
kumargandhi.in › 2022 › 07 › 21 › export-html-content-to-pdf-file-in-angular-using-jspdf
Export html content to PDF file in Angular using JSPDF. – Kumar Gandhi Koppolu
July 21, 2022 - We want to export the above Html content to the PDF file. But the · html method in the JSPDF is expecting HTMLElement so to get this object instance of our html content we need to declare the above div element in our component by referencing the id
Find elsewhere
🌐
Expert Code Blog
expertcodeblog.wordpress.com › 2018 › 07 › 24 › angular-html-to-pdf
Angular: HTML to PDF with htm2canvas and jsPDF | Expert Code Blog
August 3, 2018 - After we had import the npm packages of this tool, into our class we can create a method that esports the HTLM content into PDF: import * as jsPDF from 'jspdf' import * as html2canvas from "html2canvas"; export class MyComponent { public printPdf() ...
🌐
Medium
pankaj-kumar.medium.com › how-to-convert-html-page-to-pdf-in-angular-application-4d83956432d6
How To Convert HTML Page To PDF In Angular Application | by Pankaj Kumar | Medium
December 6, 2024 - In the above file, At the top we have imported the jspdf and html2canvas library in our ts file. Below that we have a method which we will call on button click from html file, With in the method we are getting the content from the id contentToConvert and converting it to the pdf file. <!--The content below is only a placeholder and can be replaced.--> <div> <strong>Html To PDF in Angular Application</strong> </div> <div> <input type="button" value="Convert" (click)="convetToPDF()"/> </div> <pre> <div id="content"> <div></div> </div> <div> <table id="contentToConvert"> <tbody> <tr> <th>Company<
🌐
Phppot
phppot.com › javascript › jspdf-html-example
jsPDF HTML Example with html2canvas for Multiple Pages PDF - Phppot
February 24, 2024 - These are the library documentation links that guide to creating PDFs from HTML. ... The jsPDF core alone has many features to create PDF documents on the client side.
🌐
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. ... Like Loading... Hi! I’m Kumar and I live in Bangalore (IN) with my family. By profession I’m a Web Developer experienced in a vast variety of frameworks and technologies, like, HTML, CSS, JavaScript, Angular, Bootstrap…
🌐
Google Groups
groups.google.com › g › angular › c › bZef6hlcVX0
Is it possible to export HTML to PDF or generate pdf in AngularJs without Using Other JS reference like jSPDF, PHANtom,etc
How you done with jsPdf. I tried but IE blocking blob url. here below is my code - viewriskId - div element ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... I have done it like this but the problem is I have html table and i am using highcharts to show graphs on the basis of data. so when i try to export it in pdf my graphs are not showing.
🌐
ItSolutionstuff
itsolutionstuff.com › post › angular-convert-html-to-pdf-file-exampleexample.html
Angular Convert HTML to PDF File Example - ItSolutionstuff.com
October 20, 2023 - angular html to pdf, angular generate ... 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....
🌐
Plunker
embed.plnkr.co › plunk › bvcYhd
jsPDF example from ng-bind-html - Plunker
</b>"; $scope.click = function() { // console.log("button clicked"); // $scope.order.showPopupAddedToCart = !$scope.order.showPopupAddedToCart; 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'); }; $scope.HTMLclick = function() { console.log("starting HTMLclick"); var pdf = new jsPDF('p', 'pt', 'letter'); // source can be HTML-formatted string, or a reference // to an actual DOM element from which the text will be scraped.
🌐
jsPDF
artskydj.github.io › jsPDF › docs › jsPDF.html
jsPDF - Documentation
Adds a rectangle with rounded corners to PDF. ... Saves as PDF document. An alias of jsPDF.output('save', 'filename.pdf').
🌐
npm
npmjs.com › package › jspdf
jspdf - npm
1 month ago - PDF Document creation from JavaScript. Latest version: 3.0.4, last published: 17 days ago. Start using jspdf in your project by running `npm i jspdf`. There are 2036 other projects in the npm registry using jspdf.
      » npm install jspdf
    
Published   Nov 19, 2025
Version   3.0.4
🌐
C# Corner
c-sharpcorner.com › article › convert-html-to-pdf-using-angular-6
Convert HTML To PDF Using Angular 6
May 7, 2020 - import { Component, OnInit, ElementRef ,ViewChild} from '@angular/core'; ... } As you can see in the above code snippet, I have created one method called captureScreen(), which generates document and will add image into the document and lastly, will save it on a local system. For that, I have provided a few settings, like image height, width, left margin, etc. JsDF has a syntax that decides its layout when it generates the PDF. ... ); After initialization of JsPDF, I am going to put the image into my document, using pdf.addImage(), which is used to use the image into our document.