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>
🌐
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.
Discussions

Can I use html() multiple times in a PDF
I want to export a PDF in a function. At the same time, the content inside is generated by using .html() many times. If only once .html() there is no problem at all. But I found that I used it twic... More on github.com
🌐 github.com
25
December 3, 2020
Converting HTML PAGE TO PDF using jsPDF
Puppeteer under nodeJS works well (uses headless chrome). There’s also wkhtmltopdf. More on reddit.com
🌐 r/Frontend
4
8
July 18, 2024
Add a font to PDF with jsPDF library
🌐 r/learnjavascript
1
2
July 26, 2024
Use jsPDF to generate the PDF of a map on HTML using a Geoserver

So I tried using html2canvas aside from jsPDF, I found this method

function genPDF(){ html2canvas(document.body,{ onrendered: function(canvas){ var img = canvas.toDataURL("image/png"); var doc = new jsPDF(); doc.addImage(img,'JPEG',20,20); doc.save('testo.pdf') } }); }

To add to the javascript, just add this to the html

<a href="javascript:genPDF()">Descargar</a>

...and it still doesn't work.

I want to rip my hair out, I don't know what I'm doing wrong.

More on reddit.com
🌐 r/learnprogramming
2
1
August 12, 2014
🌐
Phppot
phppot.com › javascript › html-to-pdf-in-javascript-using-jspdf
HTML to PDF in Javascript using jsPDF with Example Download - Phppot
December 3, 2022 - 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.
🌐
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 ...
🌐
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.
🌐
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.
Find elsewhere
🌐
HackMD
hackmd.io › @n6kGXbvAST2zb6hPLZ6sNQ › HJTVYZz8n
PDF Generation using jsPDF + html2canvas - HackMD
May 31, 2023 - ```javascript /** define your group ... document.getElementById(id); return html2canvas(element as HTMLElement); } ); const pdf = new jsPDF('p', 'pt', 'a4'); canvases.forEach(canvas => { // print canvas onto pdf // check if print the ...
🌐
Medium
medium.com › storygu-teemo › เปลี่ยน-html-เป็น-pdf-แบบ-ez-mid-ด้วย-jspdf-31caeb4fd74e
เปลี่ยน HTML เป็น PDF แบบ EZ Mid ด้วย JsPdf | by StoryGu
June 26, 2018 - เปลี่ยน HTML เป็น PDF แบบ EZ Mid ด้วย JsPdf. สำหรับบางครั้งที่เรามีงานลูกค้าอย่างให้ export ตาราง หรือหน้าจอมาให้แล้วบอกข...
🌐
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 the next example, we demonstrate how to generate a PDF based on user input. Here, a form is used to collect user data, and upon submission, jsPDF (in combination with the AutoTable plugin) creates a neatly formatted PDF. Fill out the form below and click Generate PDF to see jsPDF in action: Below is a complete code demonstrating how to generate a PDF from a form using jsPDF. ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PDF Form</title> <style> /* Form styling */ body { font-family: Arial, sans-serif; padding: 30px; } .container { max-width: 500px; background-col
🌐
GitHub
github.com › parallax › jsPDF
GitHub - parallax/jsPDF: Client-side JavaScript PDF generation for everyone.
Client-side JavaScript PDF generation for everyone. - parallax/jsPDF
Starred by 30.9K users
Forked by 4.8K users
Languages   JavaScript 96.4% | TypeScript 2.4% | HTML 1.2%
🌐
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.
🌐
CodePen
codepen.io › raghavajoijode › pen › zpdqrV
html to pdf using jsPDF
$('.cmd').click(function () { var ...3').text($addr.find('h3').text()); pdf.fromHTML($temp.html(), 15, 15, { 'width': 170, 'elementHandlers':specialElementHandlers } ); pdf.save('sample-file.pdf'); }); !...
🌐
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.
🌐
Phppot
phppot.com › javascript › jspdf-html-example
jsPDF HTML Example with html2canvas for Multiple Pages PDF - Phppot
February 24, 2024 - It calls the jsPDF .html() function and invokes a callback to output the PDF. In a previous code, we have seen some small examples of converting HTML to PDF using the jsPDF library.
🌐
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.
🌐
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. It can be used for web scraping, automated testing, and generating PDFs from web pages. jsPDF: A JavaScript library that generates PDFs directly from JavaScript.
🌐
YouTube
youtube.com › coding shiksha
jsPDF Tutorial | Converting HTML To PDF in jsPDF - YouTube
jsPDF Tutorial | Converting HTML To PDF in jsPDF For Blogging Tutorials and My Courses Visit official site https://www.codingshiksha.com subscribe the channe...
Published   May 16, 2019
Views   24K
🌐
GitHub
github.com › parallax › jsPDF › issues › 3074
Can I use html() multiple times in a PDF · Issue #3074 · parallax/jsPDF
December 3, 2020 - At the same time, if I continue to use html() in html() callback, I can write the content into PDF, but in the way of covering the previous one.
Published   Jan 25, 2021