🌐
npm
npmjs.com › package › jspdf
jspdf - npm
PDF Document creation from JavaScript. Latest version: 3.0.4, last published: 25 days ago. Start using jspdf in your project by running `npm i jspdf`. There are 2050 other projects in the npm registry using jspdf.
      » npm install jspdf
    
Published   Nov 19, 2025
Version   3.0.4
🌐
Phppot
phppot.com › javascript › html-to-pdf-in-javascript-using-jspdf
HTML to PDF in Javascript using jsPDF with Example Download - Phppot
It reads the HTML file and sends the content to the jsPDF-dependent library function. It uses HTML content to create the output PDF. The node_modules contain all the required dependencies of the jsPDF library.
🌐
npm
npmjs.com › package › html-to-pdf-js
html-to-pdf-js - npm
Client-side HTML-to-PDF rendering using pure JS. Latest version: 0.9.3, last published: 5 years ago. Start using html-to-pdf-js in your project by running `npm i html-to-pdf-js`. There are 2 other projects in the npm registry using html-to-pdf-js.
      » npm install html-to-pdf-js
    
Published   Jul 24, 2020
Version   0.9.3
Author   Erik Koopmans
🌐
html2pdf.js
ekoopmans.github.io › html2pdf.js
html2pdf.js | Client-side HTML-to-PDF rendering using pure JS.
html2pdf.js depends on the external packages html2canvas and jsPDF. These dependencies are automatically loaded when using NPM or the bundled package.
🌐
npm
npmjs.com › package › jspdf-html2canvas
jspdf-html2canvas - npm
Latest version: 1.5.2, last published: 2 years ago. Start using jspdf-html2canvas in your project by running `npm i jspdf-html2canvas`. There are 7 other projects in the npm registry using jspdf-html2canvas.
      » npm install jspdf-html2canvas
    
Published   Feb 27, 2024
Version   1.5.2
Author   johnnywang
🌐
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 - Now, we only need to set our custom HTML content, which will be used to generate the PDF. 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.
🌐
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. You can also check our jsPDF full documentation here. jsPDF is the most popular PDF library on npm for generating PDFs purely in JavaScript.
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>
🌐
npm
npmjs.com › search
html2pdf - npm search
Simple HTML-to-PDF using html2canvas and jsPDF.
Find elsewhere
🌐
Nutrient
nutrient.io › blog › sdk › how to convert html to pdf using react
Generate PDFs from HTML in React with jsPDF
May 14, 2025 - Step‑by‑step instructions for converting HTML/JSX to professional PDFs in React using jsPDF — setup, custom fonts, multipage output, and common pitfalls.
🌐
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%
🌐
CodePen
codepen.io › REMCOOLE › pen › xxOGoKo
html to pdf using jsPDF
$('.cmd').click(function () { var pdf = new jsPDF(); var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; var $addr = $(this).closest('.res').find('.content'); var $temp = $('.content-template'); $temp.find('h3').text($addr.find('h3').text()); pdf.fromHTML($temp.html(), 15, 15, { 'width': 170, 'elementHandlers':specialElementHandlers } ); pdf.save('sample-file.pdf'); }); !
🌐
npm
npmjs.com › package › html2pdf.js › v › 0.9.0
html2pdf.js - npm
Client-side HTML-to-PDF rendering using pure JS. Latest version: 0.12.1, last published: 3 months ago. Start using html2pdf.js in your project by running `npm i html2pdf.js`. There are 210 other projects in the npm registry using html2pdf.js.
      » npm install html2pdf.js
    
Published   Sep 17, 2025
Version   0.9.0
Author   Erik Koopmans
🌐
npm
npmjs.com › package › jspdf-autotable
jspdf-autotable - npm
This jsPDF plugin adds the ability to generate PDF tables either by parsing HTML tables or by using Javascript data directly.
      » npm install jspdf-autotable
    
Published   Feb 26, 2025
Version   5.0.2
Author   Simon Bengtsson
🌐
Templated
templated.io › blog › how-to-convert-html-to-pdf-with-nodejs
How To Convert HTML to PDF with Node.js
January 27, 2024 - This guide provides a step-by-step approach to converting HTML to PDF using Node.js, covering popular libraries like Puppeteer, jsPDF, and html-pdf.
🌐
RisingStack
blog.risingstack.com › home › hírek, események › puppeteer html to pdf generation with node.js
Puppeteer HTML to PDF Generation with Node.js - RisingStack Engineering
October 15, 2025 - This method is plain and simple: create a screenshot from the page, and put it in a PDF file. Pretty straightforward. We used two packages for this approach: Html2canvas, to make a screenshot from the DOM jsPdf, a library to generate PDF
🌐
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
🌐
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.