How To Convert HTML To PDF Using JavaScript

In this article, we will see how to convert HTML to PDF using JavaScript. PDF file format is very useful to download bulk data in the web application. It helps the user to download dynamic content in file format for offline use with export to PDF functionality. JavaScript is the easiest way to convert HTML to PDF and there are various JavaScript library is available for generating PDF from HTML. jsPDF is one of the best libraries to convert HTML to PDF using JavaScript.

So, let's see generate PDF using javascript, convert HTML to PDF using jquery, jspdf HTML to PDF, HTML to PDF javascript, jquery convert HTML to PDF, jspdf convert HTML to PDF.

Example: How To Convert HTML To PDF Using JavaScript

First, we will create an HTML Page for converting the HTML to PDF. Add the following code to your HTML page.

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



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  

    $('#create_pdf').on('click', function () {  
    function createPDF() {  
        getCanvas().then(function (canvas) {  
             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.