jsPDF is able to use plugins. In order to enable it to print HTML, you have to include certain plugins and therefore have to do the following:

  1. Go to https://github.com/MrRio/jsPDF and download the latest Version.
  2. Include the following Scripts in your project:
  • jspdf.js
  • jspdf.plugin.from_html.js
  • jspdf.plugin.split_text_to_size.js
  • jspdf.plugin.standard_fonts_metrics.js

If you want to ignore certain elements, you have to mark them with an ID, which you can then ignore in a special element handler of jsPDF. Therefore your HTML should look like this:

<!DOCTYPE html>
<html>
  <body>
    <p id="ignorePDF">don't print this to pdf</p>
    <div>
      <p><font size="3" color="red">print this to pdf</font></p>
    </div>
  </body>
</html>

Then you use the following JavaScript code to open the created PDF in a PopUp:

var doc = new jsPDF();          
var elementHandler = {
  '#ignorePDF': function (element, renderer) {
    return true;
  }
};
var source = window.document.getElementsByTagName("body")[0];
doc.fromHTML(
    source,
    15,
    15,
    {
      'width': 180,'elementHandlers': elementHandler
    });

doc.output("dataurlnewwindow");

For me this created a nice and tidy PDF that only included the line 'print this to pdf'.

Please note that the special element handlers only deal with IDs in the current version, which is also stated in a GitHub Issue. It states:

Because the matching is done against every element in the node tree, my desire was to make it as fast as possible. In that case, it meant "Only element IDs are matched" The element IDs are still done in jQuery style "#id", but it does not mean that all jQuery selectors are supported.

Therefore replacing '#ignorePDF' with class selectors like '.ignorePDF' did not work for me. Instead you will have to add the same handler for each and every element, which you want to ignore like:

var elementHandler = {
  '#ignoreElement': function (element, renderer) {
    return true;
  },
  '#anotherIdToBeIgnored': function (element, renderer) {
    return true;
  }
};

From the examples it is also stated that it is possible to select tags like 'a' or 'li'. That might be a little bit to unrestrictive for the most usecases though:

We support special element handlers. Register them with jQuery-style ID selector for either ID or node name. ("#iAmID", "div", "span" etc.) There is no support for any other type of selectors (class, of compound) at this time.

One very important thing to add is that you lose all your style information (CSS). Luckily jsPDF is able to nicely format h1, h2, h3 etc., which was enough for my purposes. Additionally it will only print text within text nodes, which means that it will not print the values of textareas and the like. Example:

<body>
  <ul>
    <!-- This is printed as the element contains a textnode -->        
    <li>Print me!</li>
  </ul>
  <div>
    <!-- This is not printed because jsPDF doesn't deal with the value attribute -->
    <input type="text" value="Please print me, too!">
  </div>
</body>
Answer from snrlx on Stack Overflow
๐ŸŒ
StackBlitz
stackblitz.com โ€บ edit โ€บ angular-jspdf-base64
Angular HTML to PDF using JSPDF - StackBlitz
Covered things - convert HTML Page to PDF and download it - Upload PDF in directory using Base64 in API
Top answer
1 of 16
325

jsPDF is able to use plugins. In order to enable it to print HTML, you have to include certain plugins and therefore have to do the following:

  1. Go to https://github.com/MrRio/jsPDF and download the latest Version.
  2. Include the following Scripts in your project:
  • jspdf.js
  • jspdf.plugin.from_html.js
  • jspdf.plugin.split_text_to_size.js
  • jspdf.plugin.standard_fonts_metrics.js

If you want to ignore certain elements, you have to mark them with an ID, which you can then ignore in a special element handler of jsPDF. Therefore your HTML should look like this:

<!DOCTYPE html>
<html>
  <body>
    <p id="ignorePDF">don't print this to pdf</p>
    <div>
      <p><font size="3" color="red">print this to pdf</font></p>
    </div>
  </body>
</html>

Then you use the following JavaScript code to open the created PDF in a PopUp:

var doc = new jsPDF();          
var elementHandler = {
  '#ignorePDF': function (element, renderer) {
    return true;
  }
};
var source = window.document.getElementsByTagName("body")[0];
doc.fromHTML(
    source,
    15,
    15,
    {
      'width': 180,'elementHandlers': elementHandler
    });

doc.output("dataurlnewwindow");

For me this created a nice and tidy PDF that only included the line 'print this to pdf'.

Please note that the special element handlers only deal with IDs in the current version, which is also stated in a GitHub Issue. It states:

Because the matching is done against every element in the node tree, my desire was to make it as fast as possible. In that case, it meant "Only element IDs are matched" The element IDs are still done in jQuery style "#id", but it does not mean that all jQuery selectors are supported.

Therefore replacing '#ignorePDF' with class selectors like '.ignorePDF' did not work for me. Instead you will have to add the same handler for each and every element, which you want to ignore like:

var elementHandler = {
  '#ignoreElement': function (element, renderer) {
    return true;
  },
  '#anotherIdToBeIgnored': function (element, renderer) {
    return true;
  }
};

From the examples it is also stated that it is possible to select tags like 'a' or 'li'. That might be a little bit to unrestrictive for the most usecases though:

We support special element handlers. Register them with jQuery-style ID selector for either ID or node name. ("#iAmID", "div", "span" etc.) There is no support for any other type of selectors (class, of compound) at this time.

One very important thing to add is that you lose all your style information (CSS). Luckily jsPDF is able to nicely format h1, h2, h3 etc., which was enough for my purposes. Additionally it will only print text within text nodes, which means that it will not print the values of textareas and the like. Example:

<body>
  <ul>
    <!-- This is printed as the element contains a textnode -->        
    <li>Print me!</li>
  </ul>
  <div>
    <!-- This is not printed because jsPDF doesn't deal with the value attribute -->
    <input type="text" value="Please print me, too!">
  </div>
</body>
2 of 16
94

This is the simple solution. This works for me. You can use the javascript print concept and simple save this as pdf.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("#btnPrint").live("click", function () {
            var divContents = $("#dvContainer").html();
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>DIV Contents</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        });
    </script>
</head>
<body>
    <form id="form1">
    <div id="dvContainer">
        This content needs to be printed.
    </div>
    <input type="button" value="Print Div Contents" id="btnPrint" />
    </form>
</body>
</html>
๐ŸŒ
StackBlitz
stackblitz.com โ€บ edit โ€บ html-to-jspdf
Html To Jspdf - StackBlitz
Starter project for Angular apps that exports to the Angular CLI
๐ŸŒ
Pdfnoodle
pdfnoodle.com โ€บ blog โ€บ generating-pdfs-from-html-with-jspdf
Generating PDFs from HTML with jsPDF and javascript
January 30, 2025 - In this guide, weโ€™ll explore how to convert HTML to PDF using jsPDF, share best practices for styling and performance, and compare jsPDF to more advanced tools like Playwright, Puppeteer, or dedicated PDF APIs.
Find elsewhere
๐ŸŒ
Phppot
phppot.com โ€บ javascript โ€บ html-to-pdf-in-javascript-using-jspdf
HTML to PDF in Javascript using jsPDF with Example Download - Phppot
This example is for simply converting HTML to PDF in JavaScript with few lines of code. It uses the jsPDF library to build a custom PDF generator tool on the client-side.
๐ŸŒ
StackBlitz
stackblitz.com โ€บ edit โ€บ angular-html-to-pdf
Angular Html To Pdf - StackBlitz
Starter project for Angular apps that exports HTML to PDF using jsPDF plugin
๐ŸŒ
GitHub
github.com โ€บ airarrazaval โ€บ html2pdf
GitHub - airarrazaval/html2pdf: HTML to PDF library
html2pdf depends on the external packages html2canvas, jsPDF, and native-promise-only. These dependencies are automatically loaded when using NPM or the bundled package. When submitting an issue, please provide reproducible code that highlights the issue, preferably by creating a fork of this template in StackBlitz (which has html2pdf already loaded).
Starred by 16 users
Forked by 20 users
Languages ย  JavaScript
๐ŸŒ
Decentro
decentro.tech โ€บ blog โ€บ engineering & apis โ€บ jspdf: what is it & how to use it to generate pdf from html
JsPDF: What Is It & How To Use It To Generate PDF from HTML - Decentro
June 12, 2024 - This makes it an ideal choice for developers who need a flexible and cost-effective solution for generating PDFs from their web applications. Today, we will use jsPDF to download an HTML file as a PDF, images, colors & backgrounds.
๐ŸŒ
html2pdf.js
ekoopmans.github.io โ€บ html2pdf.js
html2pdf.js | Client-side HTML-to-PDF rendering using pure JS.
html2pdf.js converts any webpage or element into a printable PDF entirely client-side using html2canvas and jsPDF.
๐ŸŒ
HackMD
hackmd.io โ€บ @n6kGXbvAST2zb6hPLZ6sNQ โ€บ HJTVYZz8n
PDF Generation using jsPDF + html2canvas - HackMD
--- title: PDF Generation using jsPDF + html2canvas authors: joanne-w --- ## Overview This document provides a guide on how to generate PDF in javascript using the libraries `jsPDF` and `html2canvas`. Our goals includes convert HTML content into PDF file with the ability to customize the layout and handle page splitting.
๐ŸŒ
Phppot
phppot.com โ€บ javascript โ€บ jspdf-html-example
jsPDF HTML Example with html2canvas for Multiple Pages PDF - Phppot
The latest version replaces the fromHTML plugin with the html.js plugin to convert HTML to PDF. Above all, jsPDF depends on html2canvas for generating a PDF document.
๐ŸŒ
StackBlitz
stackblitz.com โ€บ edit โ€บ angular-ivy-czctzx
js-pdf html2canvas - StackBlitz
StackBlitz is the collaborative browser-based IDE for web developers. StackBlitz eliminates time-consuming local configuration and lets developers spend more time building.
๐ŸŒ
OpenReplay
blog.openreplay.com โ€บ generating-a-pdf-from-a-div-using-the-jspdf-library
Generating a PDF from a div using the jsPDF Library
Next, we will write javascript code to download the PDF using the jsPDF library we previously setup using the instructions below: Set up an object called options that contains various configuration settings for the HTML-to-PDF conversion process.