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
🌐
CodePen
codepen.io › REMCOOLE › pen › xxOGoKo
html to pdf using jsPDF
<div id="main"> <div class="res"> <div class="content"> <h3>Sample1 h3 tag</h3> </div> <button class="cmd">Generate PDF</button> </div> <div class="res"> <div class="content"> <h3>Sample2 h3 tag</h3> </div> <button class="cmd">Generate PDF</button> </div> <div class="res"> <div class="content"> <h3>Sample3 h3 tag</h3> </div> <button class="cmd">Generate PDF</button> </div> </div> <div class="content-template"> <h3>Sample2 h3 tag</h3> <p>Sample pararaph €€€€€</p> </div> <!--Add External Libraries - JQuery and jspdf check out url - https://scotch.io/@nagasaiaytha/generate-pdf-from-html-using-jquery-and-jspdf --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script> ! CSS Options ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-add-html-and-css-into-pdf
How to Add HTML and CSS into PDF ? - GeeksforGeeks
Example: In this example, we will again see how we will convert the HTML and CSS code into a pdf document using the html2pdf library in javascript. It uses various other libraries like jsPDF, html2canvas, and others to convert the HTML and CSS document into pdf.
Published   July 23, 2025
🌐
YouTube
youtube.com › watch
html to pdf with css example using jspdf - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published   January 18, 2020
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-html-and-css-to-pdf
How to add HTML and CSS to PDF?
In this tutorial, we will learn how we can add HTML and CSS to PDF. The HTML and CSS construct the webpages with styles and designs. We can save that webpage as a PDF file. Creating a PDF from scratch using vanilla JavaScript makes it very difficult,
🌐
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.
🌐
Phppot
phppot.com › javascript › jspdf-html-example
jsPDF HTML Example with html2canvas for Multiple Pages PDF - Phppot
In a previous code, we have seen some small examples of converting HTML to PDF using the jsPDF library. This example HTML has the content target styled with internal CSS properties.
Find elsewhere
🌐
Pdfnoodle
pdfnoodle.com › blog › generating-pdfs-from-html-with-jspdf
Generating PDFs from HTML with jsPDF and javascript
January 30, 2025 - jsPDF is the most popular PDF library on npm for generating PDFs purely in JavaScript. Libraries like Playwright or Puppeteer are widely used for rendering HTML into PDFs via headless browser instances.
🌐
Nutrient
nutrient.io › blog › sdk › how to convert html to pdf using react
Generate PDFs from HTML in React with jsPDF
May 14, 2025 - This guide shows how to import jsPDF(opens in a new tab), reference your JSX with useRef, and export a polished PDF — plus tricks for custom page size, multiple pages, and custom fonts. jsPDF works for basic needs but lacks external CSS support and requires a font converter for non‑standard fonts. 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 add password protection and annotations.
🌐
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.
🌐
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 createPDF() { getCanvas().then(function (canvas) { var img = canvas.toDataURL("image/png"), doc = new jsPDF({ unit: 'px', format: 'a4' }); doc.addImage(img, 'JPEG', 20, 20); doc.save('bhavdip-html-to-pdf.pdf'); form.width(cache_width); }); } // create canvas object function getCanvas() { form.width((a4[0] * 1.33333) - 80).css('max-width', 'none'); return html2canvas(form, { imageTimeout: 2000, removeContainer: true }); } }()); </script> Here is final html file ·
🌐
GitHub
github.com › parallax › jsPDF › issues › 3532
jspdf.html() doesn't render correctly · Issue #3532 · parallax/jsPDF
December 2, 2022 - I have read and understood the contribution guidelines. jsPDF version is 2.5.1 I'm just performing a simple jspdf.html() call but the rendered PDF does not match the rendered HTML. I am using Tailwind CSS in the HTML but I have also conv...
Published   Dec 02, 2022
🌐
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

🌐
Medium
medium.com › @techsolutionstuff › how-to-convert-html-to-pdf-using-jspdf-c15ccd9435a3
How To Convert HTML To PDF Using jsPDF | Medium
January 27, 2023 - <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 jsPDF({ unit: 'px', format: 'a4' }); doc.addImage(img, 'JPEG', 20, 20); doc.save('tech-html-to-pdf.pdf'); form.width(cache_width); }); } function getCanvas() { form.width((a4[0] * 1.33333) - 80).css('max-width', 'none'); return html2canvas(form, { imageTimeout: 2000, removeContainer: true }); } }); </script>
🌐
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 - <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 jsPDF({ unit: 'px', format: 'a4' }); doc.addImage(img, 'JPEG', 20, 20); doc.save('tech-html-to-pdf.pdf'); form.width(cache_width); }); } function getCanvas() { form.width((a4[0] * 1.33333) - 80).css('max-width', 'none'); return html2canvas(form, { imageTimeout: 2000, removeContainer: true }); } }); </script>
🌐
CodePen
codepen.io › amkid › pen › qKYwXo
jsPDF from html
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
🌐
C# Corner
c-sharpcorner.com › article › how-to-convert-html-to-pdf-with-css-and-svgchart-using-jquery
How To Convert HTML To PDF With CSS And SVG(Chart) Using jQuery
August 1, 2019 - In this blog, I will explain how to make a PDF of my HTML page with CSS and SVG (since all charts give the SVG object). I will use jspdf.debug.js, html2canvas.min.js, and canvg.min.js in this article so it is a combination of all three JavaScript files and makes a perfect PDF file.
🌐
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 - Turns out jsPDF does not work with CSS. And to make it work with CSS, it was to use html2canvas. And that’s what html2pdf had been doing all along.