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
🌐
npm
npmjs.com › package › html-to-pdf-js
html-to-pdf-js - npm
Update your CI/CD workflows to avoid disruption. Learn more.× ... html2pdf.js converts any webpage or element into a printable PDF entirely client-side using html2canvas and jsPDF.
      » npm install html-to-pdf-js
    
Published   Jul 24, 2020
Version   0.9.3
Author   Erik Koopmans
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>
People also ask

How can I convert HTML with JavaScript to PDF in a .NET application?
You can use IronPDF's `RenderHtmlAsPdf` method to convert HTML content with JavaScript into PDFs. By enabling JavaScript execution in the rendering options, you can ensure that dynamic content is properly processed during the conversion.
🌐
ironpdf.com
ironpdf.com › how-tos › javascript (custom render delays)
(Step by Step) Using JavaScript in HTML to PDF in C# | IronPDF
What are some potential challenges when using JavaScript with HTML to PDF conversion?
Some complex JavaScript frameworks might encounter issues with IronPDF due to memory allocation constraints during JavaScript execution. It is recommended to test and optimize scripts to ensure compatibility.
🌐
ironpdf.com
ironpdf.com › how-tos › javascript (custom render delays)
(Step by Step) Using JavaScript in HTML to PDF in C# | IronPDF
What steps are necessary to enable JavaScript execution in HTML to PDF conversion?
To enable JavaScript execution in IronPDF, set the `EnableJavaScript` property to `true` within the rendering options. This allows the PDF conversion to process any JavaScript present in the HTML.
🌐
ironpdf.com
ironpdf.com › how-tos › javascript (custom render delays)
(Step by Step) Using JavaScript in HTML to PDF in C# | IronPDF
🌐
Nutrient
nutrient.io › blog › sdk › html to pdf in javascript
Convert HTML to PDF with JavaScript: Top libraries compared
August 22, 2025 - Compare four JavaScript HTML-to-PDF solutions: html2pdf.js, pdfmake, Playwright, and Nutrient. Learn how each works, their tradeoffs, and which fits your use case.
🌐
DEV Community
dev.to › awanshrestha › html-to-pdf-javascript-example-with-code-1eha
HTML to PDF JavaScript – Example with Code - DEV Community
September 17, 2025 - If you're working with .NET/C#, IronPDF is a library that handles HTML to PDF conversion on the server side. IronPDF solves similar issues I’m about to mention in this article: The CSS not loading: IronPDF uses Chrome's rendering engine, so external CSS works as expected. The text not being selectable: IronPDF generates real text in PDFs, not canvas images. Alignment issue with jsPDF: Full CSS3 support means your divs stay where they should.
🌐
PDF Generation API
pdfgeneratorapi.com › home › 3 ways to generate pdf from html with javascript
3 Ways to Generate PDF from HTML with JavaScript - PDF Generator API
May 8, 2025 - HTML2PDF: A library that allows you to convert HTML content into PDF files using JavaScript. Puppeteer: A Node.js library that provides a high-level API to control headless Chrome or Chromium browsers.
🌐
PDFreactor
pdfreactor.com › home › html to pdf javascript: use pdfreactor & javascript to create pdf
HTML to PDF JavaScript: Use PDFreactor & JavaScript to create PDF
October 9, 2024 - Convert HTML to PDF JavaScript with PDFreactor! The JavaScript engine allows to transform your JavaScript based HTML into printable PDF.
🌐
html2pdf.js
ekoopmans.github.io › html2pdf.js
html2pdf.js | Client-side HTML-to-PDF rendering using pure JS.
Skip to the content. ... html2pdf.js converts any webpage or element into a printable PDF entirely client-side using html2canvas and jsPDF.
Find elsewhere
🌐
IronPDF
ironpdf.com › how-tos › javascript (custom render delays)
(Step by Step) Using JavaScript in HTML to PDF in C# | IronPDF
This feature allows you to debug and log console messages during the PDF rendering process. Yes, IronPDF can render charts created with D3.js by enabling JavaScript execution in the rendering options and using methods like `RenderHtmlFileAsPdf` to convert HTML files containing chart scripts into PDFs.
Published   July 23, 2025
🌐
Nutrient
nutrient.io › blog › sdk › how to convert html to pdf using html2pdf
Convert HTML to PDF in JavaScript with html2pdf.js
May 1, 2025 - Learn how to convert HTML to PDF using html2pdf.js with this JavaScript-focused guide. Includes implementation tips, customization options, and best practices for fast, client-side PDF generation — no server required.
🌐
CodexWorld
codexworld.com › home › convert html to pdf using javascript
Convert HTML to PDF using JavaScript - CodexWorld
July 10, 2024 - If you want a client-side solution to generate PDF documents, JavaScript is the easiest way to convert HTML to PDF. There are various JavaScript library is available to generate PDF from HTML. The jsPDF is one of the best library to convert HTML to PDF using JavaScript.
🌐
Medium
medium.com › @livajorge7 › converting-html-to-pdf-with-javascript-best-practices-and-techniques-f7daf89306e
Converting HTML to PDF with JavaScript: Best Practices and Techniques | by liva jorge | Medium
February 27, 2023 - One of the most popular libraries for HTML to PDF conversion is jsPDF. It’s an open-source library that provides a simple API to generate PDF documents from HTML content. jsPDF supports various document formats, including A4, Letter, and Legal.
🌐
Geekflare
geekflare.com › development › how to convert html to pdf using javascript
How to Convert HTML to PDF using JavaScript
June 5, 2025 - It also might not be immediately obvious to your users that you want them to print the PDF version. An easier method is to use the jsPDF library. This library lets you convert your HTML document to PDF and download it immediately.
🌐
Phppot
phppot.com › javascript › html-to-pdf-in-javascript-using-jspdf
HTML to PDF in Javascript using jsPDF with Example Download - Phppot
Step1: Read HTML data and show preview in the UI. Step2: Generate PDF and give the option to download the output file. It handles JavaScript validation and file type restrictions before starting PDF generation.
🌐
LogRocket
blog.logrocket.com › home › best html to pdf libraries for node.js
Best HTML to PDF libraries for Node.js - LogRocket Blog
November 14, 2024 - We investigate and compare four popular HTML to PDF libraries for Node.js applications: Puppeteer, jsPDF, PDFKit, and pdfmake.
🌐
npm
npmjs.com › package › html-pdf
html-pdf - npm
HTML to PDF converter that uses phantomjs. Latest version: 3.0.1, last published: 5 years ago. Start using html-pdf in your project by running `npm i html-pdf`. There are 362 other projects in the npm registry using html-pdf.
      » npm install html-pdf
    
Published   May 07, 2021
Version   3.0.1
Author   Marc Bachmann
🌐
APITemplate.io
apitemplate.io › home › convert html to pdf in node.js with 4 popular libraries (updated 2024)
Convert HTML to PDF in Node.js with 4 Popular Libraries (Updated 2024) - APITemplate.io
June 24, 2024 - Finally, we generate the PDF and close the browser. jsPDF is a JavaScript library that allows you to generate PDF files from HTML programmatically. It works in both Node.js and browser environments.
🌐
npm
npmjs.com › package › html-pdf-node
html-pdf-node - npm
Defaults to false, which will scale the content to fit the paper size. ... Promise which resolves with array of object which contains file objects with PDF buffers. ... let options = { format: 'A4' }; let file = [{ url: "https://example.com", name: 'example.pdf' }]; html_to_pdf.generatePdfs(file, options).then(output => { console.log("PDF Buffer:-", output); // PDF Buffer:- [{url: "https://example.com", name: "example.pdf", buffer: <PDF buffer>}] });
      » npm install html-pdf-node
    
Published   Dec 14, 2021
Version   1.0.8
Author   mrafiqk
🌐
Medium
weihui-guo.medium.com › which-javascript-library-should-i-use-for-client-side-html-to-pdf-conversion-77f25e40f782
Which JavaScript Library Should I Use for Client-side HTML-to-PDF Conversion? | by Weihui Guo | Medium
September 10, 2020 - Third, background-image — both jsPDF and pdfmake have trouble rendering an element with the background-image property. html2pdf.js is the only one I tested that works. Pdf.js is usually used for rendering actual PDF files. But the new release of jsPDF leveraged pdf.js to convert HTML page to PDF and allows user-defined file name for download.
🌐
GitHub
github.com › parallax › jsPDF
GitHub - parallax/jsPDF: Client-side JavaScript PDF generation for everyone.
A library to generate PDFs in JavaScript. You can catch me on twitter: @MrRio or head over to my company's website for consultancy. jsPDF is now co-maintained by yWorks - the diagramming experts.
Starred by 30.9K users
Forked by 4.8K users
Languages   JavaScript 96.4% | TypeScript 2.4% | HTML 1.2%