How To Convert HTML To PDF Using jsPDF

In this article, we will see how to convert HTML to pdf using jsPDF. Here, we will learn how to convert HTML to PDF using javascript. We are using jsPDF to generate PDF in javascript. It can generate PDFs from the client side. The jsPDF is very easy to use in javascript.

It also provides multiple options for generating PDF files. You can generate PDF files with images, and multiple pages, and also support different languages like Japanese, Arabic, Russian, etc.

Learn More: jsPDF Javascript PDF Generate.

So, let's see jsPDF HTML to pdf convert, jsPDF HTML to pdf multiple pages, convert HTML to pdf using javascript, HTML to pdf in javascript using jsPDF.

jsPDF provides two different methods to use.

  1. Install using NPM
  2. jsPDF CDN File

In this step, we will create an HTML file. So, add the following code to the file.

    <h1>How To Convert HTML To PDF Using jsPDF- Techsolutionstuff</h1>
    <form class="form">        
                    <th>Company Name</th>  
                    <th>Employee Name</th>  
        <input type="button" id="generate_pdf" value="Generate PDF">  

Now, add the style of this HTML page.

table {  
    font-family: arial, sans-serif;  
    border-collapse: collapse;  
    width: 100%;  

td {  
    border: 1px solid #dddddd;  
    text-align: left;  
    padding: 8px;  
    border: 1px solid #dddddd;  
    text-align: left;  
    padding: 8px;  
    background-color: #111;  

tr:nth-child(odd) {  
    background-color: #dddddd;  

Add the following script to the HTML page for converting it to pdf.

<script src=""></script>
<script src=""></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 () {  
    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);  
    function getCanvas() {  
        form.width((a4[0] * 1.33333) - 80).css('max-width', 'none');  
        return html2canvas(form, {  
            imageTimeout: 2000,  
            removeContainer: true  


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.