How To Convert HTML To Pdf - [Latest 2022]

Convert HTML to PDF file - On Button Click

convert html to pdf

In this article, I will tell you how you can create PDF file from HTML. You must be wondering what's new in this, we can convert HTML to PDF by simply pressing CTRL+P. My dear friend, you are right but the CTRL+P method allows you to print the entire page and not a particular section of the HTML file.

And being a website owner you are not allowing users to create PDF, CTRL+P is the normal process. To make your website interactive and attractive, you should provide such facilities to your users so that they can stay on your website for longer.

Benefits of creating PDF from HTML: You can create invoices, make special notes, and create and download PDFs of any HTML section you want. Also useful in printing users records, printing sales records etc.

If you want to create HTML to PDF converter feature on your website then you will love this article.

So, let's learn how to convert HTML to PDF.


We will convert HTML to PDF using JavaScript and jQuery.

  1. JSPDF.js
  2. html2canvas.js

Steps for converting HTML to PDF

STEP 1: Create the HTML section you want to print. Here in this example, I am taking a company employee record to create a pdf.

<div class="printable" style="max-width: none; width: 1005px;">  
  <h2 style="color: #0094ff">XYZ Ltd. Employee Record 2022-23</h2>  
  <th>Emp. No.</th>
  <th>Emp. Name</th>
  <td>Software Developer</td>
  <td>Content Writer</td>
  <td>Network Engineer</td>
  <td>Eric John</td>
  <br />
  <p><b>Note: </b>This is just a Dummy record, we created this table record for demonstration purpose.</p>

STEP 2: To make the above table looks professional, we are going to add styling to it. This is purely optional.

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

      td, th {  
          border: 1px solid #e4e6f8;  
          text-align: left;  
          padding: 8px;  

      tr:nth-child(even) {  
          background-color: #e4e1e1;  

STEP 3: Add a Print button wherever you want. This may be below or above the printable section.

<input type="button" id="create_pdf" value="Create PDF">

STEP 4: Add the following two Scripts [CDN] in your HTML file to convert HTML to PDF

<script src="" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src=""></script>

STEP 5: Add other two scripts for converting the document.

      (function () {  
           form = $('.printable'),  
           cache_width = form.width(),  
           a4 = [595.28, 841.89]; // for a4 size paper width and height  
          $('#create_pdf').on('click', function () {  
          //create pdf  
          function createPDF() {  
              getCanvas().then(function (canvas) {  
                   img = canvas.toDataURL("image/png"),  
                   doc = new jsPDF({  
                       unit: 'px',  
                       format: 'a4'  
                  doc.addImage(img, 'JPEG', 20, 20);  
          // create canvas object  
          function getCanvas() {  
              form.width((a4[0] * 1.33333) - 80).css('max-width', 'none');  
              return html2canvas(form, {  
                  imageTimeout: 2000,  
                  removeContainer: true  
   * jQuery helper plugin for examples and tests 
      (function ($) {  
          $.fn.html2canvas = function (options) {  
              var date = new Date(),  
              $message = null,  
              timeoutTimer = false,  
              timer = date.getTime();  
              html2canvas.logging = options && options.logging;  
              html2canvas.Preload(this[0], $.extend({  
                  complete: function (images) {  
                      var queue = html2canvas.Parse(this[0], images, options),  
                      $canvas = $(html2canvas.Renderer(queue, options)),  
                      finishTime = new Date();  
                      $canvas.css({ position: 'absolute', left: 0, top: 0 }).appendTo(document.body);  
                      $(window).click(function () {  
                          if (!$':visible')) {  
                              throwMessage("Canvas Render visible");  
                          } else {  
                              throwMessage("Canvas Render hidden");  
                      throwMessage('Screenshot created in ' + ((finishTime.getTime() - timer) / 1000) + " seconds<br />", 4000);                    }  
              }, options));  
              function throwMessage(msg, duration) {  
                  timeoutTimer = window.setTimeout(function () {  
                      $message.fadeOut(function () {  
                  }, duration || 2000);  
                  if ($message)  
                  $message = $('<div ></div>').html(msg).css({  
                      margin: 0,  
                      padding: 10,  
                      background: "#000",  
                      opacity: 0.7,  
                      position: "fixed",  
                      top: 10,  
                      right: 10,  
                      fontFamily: 'Tahoma',  
                      color: '#fff',  
                      fontSize: 12,  
                      borderRadius: 12,  
                      width: 'auto',  
                      height: 'auto',  
                      textAlign: 'center',  
                      textDecoration: 'none'  

Output console

html to pdf converter console

Output Screenshot of Generated PDF

html to pdf converter output_pdf
Karuna Singh

Greetings to everyone. I am Karuna Singh, I am a writer and blogger since 2018. I have written 250+ articles and generated targeted traffic. Through this blog blogEarns, I want to help many fellow bloggers at every stage of their blogging journey and create a passive income stream from their blog.

Thank you for your valuable comments. We like to hear from you.

Post a Comment (0)
Previous Post Next Post