As I know jsPDF is not working with CSS and the same issue I was facing.

To solve this issue, I used Html2Canvas. Just Add HTML2Canvas JS and then use pdf.addHTML() instead of pdf.fromHTML().

Here's my code (no other code):

 var pdf = new jsPDF('p', 'pt', 'letter');
 pdf.addHTML($('#ElementYouWantToConvertToPdf')[0], function () {
     pdf.save('Test.pdf');
 });

Best of Luck!

Edit: Refer to this line in case you didn't find .addHTML()

Answer from Mihir on Stack Overflow
🌐
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. examples/ provides more examples, including usage in Node.js (at examples/node/).
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί html β€Ί how-to-add-html-and-css-into-pdf-file
How to add HTML and CSS into PDF File ? - GeeksforGeeks
July 23, 2025 - // Loading the jsPDF library <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script> // Creating a new jsPDF object let pdf = new jsPDF(); Example: In this example, we will see how we will convert the HTML ...
🌐
Ttmind
ttmind.com β€Ί techpost β€Ί Generate-PDF-from-HTML-with-CSS-by-using-JavaScript-and-JQuery
Generate PDF from HTML with CSS by using JavaScript and JQuery
December 2, 2018 - <script> (function () { var form = $('.form'), cache_width = form.width(), a4 = [595.28, 841.89]; // for a4 size paper width and height $('#create_pdf').on('click', function () { $('body').scrollTop(0); createPDF(); }); //create pdf function ...
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-add-html-and-css-to-pdf
How to add HTML and CSS to PDF?
</p> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.2/html2pdf.bundle.js"> </script> <script> var btn = document.getElementById("btn"); var createpdf = document.getElementById("div"); var opt = { margin: 1, filename: 'pdfcreated.pdf', html2canvas: { scale: 2 }, jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' } }; btn.addEventListener("click", function() { html2pdf().set(opt).from(createpdf).save(); }); </script> </body> </html> In the above example, users can see using the html2pdf library of JavaScript. We have converted our HTML and CSS webpage into a PDF.
🌐
Slideshare
slideshare.net β€Ί home β€Ί career β€Ί introduction to html-css-javascript.pdf
Introduction to HTML-CSS-Javascript.pdf
The document provides an introduction to HTML, CSS, and JavaScript for creating web technologies. It discusses how to set up a development environment with a text editor and browser. It explains the basic structure and syntax of HTML, CSS, and JavaScript, including how to select and style elements ...
🌐
Nematrian
nematrian.com β€Ί Pages β€Ί HTMLCSSJSCombined.pdf pdf
1 HTML, CSS and JavaScript
JavaScript can be added to a webpage in one of three ways (somewhat akin to how CSS can be ... A simple example of JavaScript involves the use of the document.write method. For example, the following HTML text would return a web page the first line of which says β€œHello World (using
Find elsewhere
🌐
Joyfill
joyfill.io β€Ί blog β€Ί creating-pdfs-from-html-css-in-javascript-what-actually-works
Creating PDFs from HTML + CSS in JavaScript: What actually works - Joyfill
July 31, 2025 - Learn how to convert styled HTML and CSS into PDFs entirely in the browser using JavaScript. Compare libraries like html2pdf.js, jsPDF, and pdfmake, explore real-world trade-offs, and find out when to scale with high-level tools like Joyfill.
🌐
tutorialpedia
tutorialpedia.org β€Ί blog β€Ί jspdf-html-css
Leveraging jsPDF with HTML and CSS for PDF Generation β€” tutorialpedia.org
For example: const { jsPDF } = window.jspdf; const doc = new jsPDF(); const content = document.getElementById('longContent'); const maxHeight = doc.internal.pageSize.height - 20; // Leave some margin let currentY = 10; const lines = ...
🌐
PDF.js Express
pdfjs.express β€Ί documentation β€Ί ui-customization β€Ί customizing-styles
PDF.js Express Viewer Style Customization (Color & CSS) | Documentation
WebViewer({ licenseKey: 'YOUR_LICENSE_KEY', path: 'lib', css: 'path/to/stylesheet.css' }, document.getElementById('viewer'));
🌐
JSFiddle
jsfiddle.net β€Ί pdfjs β€Ί 9engc9mw
PDF.js 'Hello, world!' example - JSFiddle - Code Playground
Adding External Resources will no longer create a list of resources in the sidebar but will be injected as a LINK or SCRIPT tag inside of the HTML panel. The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί how-to-add-html-and-css-into-pdf
How to Add HTML and CSS into PDF ? - GeeksforGeeks
To add HTML and CSS to a PDF, different approaches can be used and they are as: ... On the client side PDF file creation is possible thanks to the jsPDF JavaScript library. It provides a simple approach for creating and downloading PDF files directly in the browser by combining HTML, CSS, and ...
Published Β  July 23, 2025
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>
🌐
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 - The following table compares PDFGeneratorAPI.com’s API with JavaScript libraries like HTML2PDF and jsPDF. Here are some best practices for optimizing PDFs when using PDF Generator API: Clean and Well-Organized HTML: Ensure your HTML structure is clean and well-organized, using semantic elements appropriately. CSS for Styling: Use CSS for styling your HTML content instead of inline styles or formatting elements like <font> or <b>.
🌐
Nutrient
nutrient.io β€Ί blog β€Ί sdk β€Ί html to pdf in javascript
Convert HTML to PDF with JavaScript: Top libraries compared
August 22, 2025 - This guide compares four JavaScript solutions for HTML-to-PDF conversion: html2pdf, pdfmake, Playwright, and Nutrient. Learn how each works, what their tradeoffs are, and which solution fits your specific use case and scale requirements. ... Simple client-side export (<100 documents/month)? β€” html2pdf.js β€” Fast setup, but limited CSS support
🌐
Mozilla
mozilla.github.io β€Ί pdf.js β€Ί getting_started
PDF.js - Getting Started
β”œβ”€β”€ build/ β”‚ β”œβ”€β”€ pdf.mjs - display layer β”‚ β”œβ”€β”€ pdf.mjs.map - display layer's source map β”‚ β”œβ”€β”€ pdf.worker.mjs - core layer β”‚ └── pdf.worker.mjs.map - core layer's source map β”œβ”€β”€ web/ β”‚ β”œβ”€β”€ cmaps/ - character maps (required by core) β”‚ β”œβ”€β”€ compressed.tracemonkey-pldi-09.pdf - PDF file for testing purposes β”‚ β”œβ”€β”€ images/ - images for the viewer and annotation icons β”‚ β”œβ”€β”€ locale/ - translation files β”‚ β”œβ”€β”€ viewer.css - viewer style sheet β”‚ β”œβ”€β”€ viewer.html - viewer layout β”‚ β”œβ”€β”€ viewer.mjs - viewer layer