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>
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Nutrient
nutrient.io โ€บ blog โ€บ sdk โ€บ how to convert html to pdf using react
Generate PDFs from HTML in React with jsPDF
May 14, 2025 - If you look at the live demo examples(opens in a new tab) of jsPDF, youโ€™ll see itโ€™s possible to convert images, font faces, font sizes, circles, rectangles, triangles, tables, lines, languages, and more into PDF format. You can also convert HTML into multiple pages with page breaks and ...
๐ŸŒ
Pdfnoodle
pdfnoodle.com โ€บ blog โ€บ generating-pdfs-from-html-with-jspdf
Generating PDFs from HTML with jsPDF and javascript
January 30, 2025 - Learn how to convert HTML to PDF using jsPDF, a popular JavaScript library. Follow our step-by-step guide to generate professional PDFs from templates.
๐ŸŒ
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.
๐ŸŒ
CodexWorld
codexworld.com โ€บ home โ€บ convert html to pdf using javascript
Convert HTML to PDF using JavaScript - CodexWorld
July 10, 2024 - HTML to PDF with JavaScript - Convert HTML content to PDF using jsPDF and jQuery. Example code to generate PDF document from HTML content of the web page in JavaScript using jsPDF library.
๐ŸŒ
MicroPyramid
micropyramid.com โ€บ blog โ€บ export-html-web-page-to-pdf-using-jspdf
Export HTML Web Page to PDF Using JsPDF | MicroPyramid
<script> function demoFromHTML() { 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.
Find elsewhere
๐ŸŒ
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'); }); !
๐ŸŒ
PDFBolt
pdfbolt.com โ€บ blog โ€บ generate-html-to-pdf-with-jspdf
Generate PDFs with jsPDF: A Complete Guide to Client-Side PDF Creation | PDFBolt
March 13, 2025 - In some cases, you may want to capture the exact look of your web page โ€“ including all the applied styles โ€“ and convert it into a PDF. By combining html2canvas with jsPDF, you can capture an HTML element as an image and then embed it into a PDF.
๐ŸŒ
Reddit
reddit.com โ€บ r/frontend โ€บ converting html page to pdf using jspdf
r/Frontend on Reddit: Converting HTML PAGE TO PDF using jsPDF
July 18, 2024 -

Hi, I am trying to create a file of my HTML into pdf but it's not coming out very nice the font size is small plus the margin from the sides is not consistent. th attached picture will give an idea how the margin from the left is so much I tried fixing but nothing worked

I am using jsPDF, is there a better library or something I can use or some other solution

๐ŸŒ
DEV Community
dev.to โ€บ jringeisen โ€บ using-jspdf-html2canvas-and-vue-to-generate-pdfs-1f8l
Using jsPDF, html2Canvas, and Vue to generate PDF's. - DEV Community
October 29, 2021 - Now, once you've created your jsPDF object you'll need to use html2canvas to create an image which can then be converted to a pdf, like so:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ html โ€บ how-to-generate-pdf-file-using-jspdf-library
Generate PDF File Using jsPDF Library - GeeksforGeeks
A button labeled "Generate PDF" is styled with a green background and hover effect, set to trigger the generatePDF() function when clicked. The jsPDF library is included via a CDN, enabling JavaScript-based PDF generation.
Published ย  July 23, 2025
๐ŸŒ
Tech Solution Stuff
techsolutionstuff.com โ€บ post โ€บ how-to-convert-html-to-pdf-using-jspdf
How To Convert HTML To PDF Using jsPDF
April 21, 2024 - Add the following script to the HTML page for converting it to pdf. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script> <script> $(document).ready(function () { var form = $('.form'), cache_width = form.width(), a4 = [595.28, 841.89]; // for a4 size paper width and height $('#generate_pdf').on('click', function () { $('body').scrollTop(0); generatePDF(); }); function generatePDF() { getCanvas().then(function (canvas) { var img = canvas.toDataURL("image/png"), doc = new j
๐ŸŒ
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%
๐ŸŒ
OpenReplay
blog.openreplay.com โ€บ generating-a-pdf-from-a-div-using-the-jspdf-library
Generating a PDF from a div using the jsPDF Library
jsPDF is a JavaScript library specifically built for client-side PDF generation. Its key features include: Client-side operation: Generates PDFs without server-side dependencies. Customization: Offers control over PDF layout, styling, and content.
๐ŸŒ
DEV Community
dev.to โ€บ hulyamasharipov โ€บ how-to-convert-html-to-pdf-using-react-37j4
How to Convert HTML to PDF Using React - DEV Community
November 28, 2023 - In this tutorial, youโ€™ll learn how to convert HTML into PDF using React, one of the most popular JavaScript libraries. To achieve this, youโ€™ll use an open source package called jsPDF, which is a client-side library that doesnโ€™t require any server-side processing.
๐ŸŒ
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.
๐ŸŒ
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').