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
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>
🌐
Mozilla
mozilla.github.io › pdf.js › examples
PDF.js - Examples
This tutorial shows how PDF.js can be used as a library in a web browser.
Discussions

Learning JavaScript Literature in PDF
This looks like a really great resource to use while learning JS. Thanks for taking the time to make it. More on reddit.com
🌐 r/Frontend
10
36
June 28, 2023
What's one of the best books in 2018 to learn JavaScript?

If you can wait a few months, I recommend reading Professional JavaScript for Web Developers 4th Edition . It's scheduled for release on July 31, 2018. The 3rd Edition was the best JavaScript book in 2012, but I would no longer buy that edition since some parts have become outdated. To shorten the waiting time you could take a look at Eloquent JavaScript 3rd Edition. It's free and from 2018.

More on reddit.com
🌐 r/javascript
75
227
September 12, 2015
🌐
PDFCOFFEE.COM
pdfcoffee.com › tutorialspoint-javascript-full-pdf-pdf-free.html
TutorialsPoint-JavaScript-Full-pdf - PDFCOFFEE.COM
Audience This tutorial has been prepared for JavaScript beginners to help them understand the basic functionality of JavaScript to build dynamic web pages and web applications. Prerequisites For this tutorial, it is assumed that the reader have ...
🌐
Dcpehvpm
dcpehvpm.org › E-Content › BCA › BCA-II › Web Technology › javascript_tutorial_Point.pdf pdf
Tutorials Point, Simply Easy Learning 1 | P a g e Javascript Tutorial
check tutorialspoint.com/javascript · What is JavaScript ? JavaScript is:  · JavaScript is a lightweight, interpreted programming language ·  · Designed for creating network-centric applications ·  · Complementary to and integrated with Java ·
🌐
Scribd
scribd.com › doc › 288930171 › Javascript-Tutorial
Javascript Tutorial | PDF | Java Script | Dynamic Web Page
Javascript Tutorial - Free download as PDF File (.pdf), Text File (.txt) or read online for free. jAVA SCRIPT
Rating: 5 ​ - ​ 7 votes
Find elsewhere
🌐
Eloquent JavaScript
eloquentjavascript.net
Eloquent JavaScript
A paper version of Eloquent JavaScript, including an additional chapter, is being brought out by No Starch Press. Code sandbox and exercise solutions · Errata for the paper book · This book as a single PDF file (& small version for mobile) This book as an EPUB file ·
🌐
FreeBookCentre
freebookcentre.net › web-books-download › JavaScript-Tutorial-by-W3schools.html
JavaScript Tutorial by W3schools | Download book PDF
JavaScript Tutorial by W3schools Download Books and Ebooks for free in pdf and online for beginner and advanced levels
🌐
JavaScript.info
javascript.info
The Modern JavaScript Tutorial
BuyEPUB/PDF · Share:24976 ★githubDiscord Chat · Main course contains 2 parts which cover JavaScript as a programming language and working with a browser. There are also additional series of thematic articles.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-tutorial
JavaScript Tutorial - GeeksforGeeks
Client Side: On the client side, JavaScript works along with HTML and CSS.
Published   September 10, 2025
🌐
PDF-LIB
pdf-lib.js.org
PDF-LIB · Create and modify PDF documents in any JavaScript environment.
import { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib'; async function modifyPdf() { const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf' const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer()) const pdfDoc = await PDFDocument.load(existingPdfBytes) const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica) const pages = pdfDoc.getPages() const firstPage = pages[0] const { width, height } = firstPage.getSize() firstPage.drawText('This text was added with JavaScript!', { x: 5, y: height / 2 + 300, size: 50, font: helveticaFont, color: rgb(0.95, 0.1, 0.1), rotate: degrees(-45), }) const pdfBytes = await pdfDoc.save() }
🌐
JavaScript Tutorial
javascripttutorial.net › home
JavaScript Tutorial
December 13, 2024 - JavaScript Tutorial provides you with many practical tutorials that help you learn JavaScript from scratch quickly and effectively.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide
JavaScript Guide - JavaScript | MDN
November 7, 2025 - The JavaScript Guide shows you how to use JavaScript and gives an overview of the language. If you need exhaustive information about a language feature, have a look at the JavaScript reference.