Code to Print and Save HTML Page as PDF using JavaScript

Code to Print and Save HTML Page as PDF using JavaScript

Being a web developer, many times you need to allow the user to print or save HTML page as PDF.

Suppose you have a web form on your website and the user has to submit the data. After submitting the form, you can allow the user to print or save the HTML page as a PDF with all the user data. A user can use this data for future reference.

Another example. If you have a tutorial on your website, you can allow a user to print the tutorial so that the user can use it offline.

A simple solution to allow the user to print or save the HTML page as PDF will be very helpful. You don’t need any external tool to convert HTML to PDF.

Code to Print and Save HTML Page as PDF

Hope you are familiar with basic HTML tags. Call the Javascript function inside the HTML to print and save the webpage as PDF.

<!DOCTYPE html>
<html>
  <body>
    <h2>Tutorial to print HTML page</h2>
    <p>Click the button to print this page.</p>

    <button onclick="printFunction()">Print</button>

    <script>
      function printFunction() { 
        window.print(); 
      }
    </script>
  </body>
</html>

Here,

  • The window.print() is the inbuilt function to print the current window.
  • The  printFunction() is user define javascript function which you call from HTML object.

By clicking on the button, the user can save the HTML page as PDF or can download it.

The print() function supports almost all the popular web browsers like Google Chrome, Firefox, Internet Explorer, Opera, and Safari (Mac and other iOS web browsers).

What’s Next?

If you are a web developer or interested in web development,  do check how the 20 best web form design practices have helped me grow professionally.

Javascript is one of the highest-paying programming languages and has very advanced programming features.

This is a simple JavaScript function to convert HTML to PDF or to print or save HTML page as PDF. You can run this code in an online HTML editor for testing.

For more of such content, stay tuned.

21 Comments

    1. Set the div for the content you want to print. And then use the write() method inside the JavaScript function.

      Let’s say…

      function printFunction() {
                  var divContents = document.getElementById("DIV_ID").innerHTML;
                  var prnt = window.open('', '', 'height=500, width=500');
                   prnt .document.write(divContents);
                  prnt .document.close();
                  prnt .print();
      }
      

      Try this and let me know if this is working for you.

      1. Hi

        Is there any other way to code for saving as pdf, rather than doing it manually?
        Means in one click, it automatically selects the ‘window. print’ and select ‘save as PDF’ and saving the PDF

        Thanks in advance.

      2. Hi
        Is there any way to ‘save as pdf ‘ programmatically using window.print()? I’ve used libraries for it. But the CSS became the issue there.

        Thanks in advance.

  1. Good evening sir

    Code to print the upload file. For example, I am uploading files means, I want to print the same file coding in HTML based using javascript.

  2. Hi
    Is there any way to ‘save as pdf ‘ programmatically using window.print()? I am using Python.

Leave a Reply

Your email address will not be published. Required fields are marked *