How To Convert HTML To PDF using JavaScript

Websolutionstuff | Aug-23-2021 | Categories : jQuery HTML

In this example 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.So here, we will generate pdf using javascript.

JavaScript is the easiest way to converting html to pdf and there are various JavaScript library is available for generating PDF from HTML. jsPDF is one of the best library to convert HTML to PDF using JavaScript.

So, let's generate HTML to PDF using JavaScript.

First creating an HTML Page for converting the HTML to PDF. Add the following code in your HTML page.

    <h1>How To Convert HTML To PDF using JavaScript - Websolutionstuff</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 in 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  


And finally you will get output like below screenshot when click Generate PDF button.

How To Convert HTML To PDF using JavaScript


You may also like :

Recommended Post
Featured Post
Laravel 8 Ajax CRUD With Yajra Datatable
Laravel 8 Ajax CRUD With Yajra...

In this tutorial, we will see laravel 8 ajax crud with yajra datatable. I will show you how to create ajax crud ope...

Read More


Laravel 8 QR Code Generator Example
Laravel 8 QR Code Generator Ex...

In this tutorial, we will see the laravel 8 QR code generator example. we will generate QR code using simpleso...

Read More


Multi Step Form Example In Laravel
Multi Step Form Example In Lar...

Today in this post we will see multi step form example in laravel, here we will create laravel multi step form example.&...

Read More


Dropdown Filter On Specific Column In Datatable
Dropdown Filter On Specific Co...

In this article, we will see how to add multiple filter dropdowns in datatable. This example is almost identical to...

Read More