All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Window Print Save As PDF JavaScript

Last Updated : Jul 1, 2023

Window Print Save As PDF JavaScript

In this tutorial we will show you the solution of window print save as pdf JavaScript, here we need to use print() method. The print() method prints the contents of the current window and it will opens the print dialog box, which lets the user to select preferred printing options.

This method called by window service object then we can choose save as pdf in designation.

Step By Step Guide On Window Print Save As PDF JavaScript :-

Here we defined button ‘Print’ with class attribute ‘print’ for access this button at script block.

In script we declared variable ‘btn’ with ‘const’ keyword for refer button ‘Print’ defined in body block by querySelector() method.

We defined function ‘printORsave()’ within that we generating print() method by using window object service. Using ‘addEventListener()’ method we initializing function ‘printORsave()’ call when user clicks on button ‘btn’.

<!DOCTYPE html>
<html>
    <head>
        <title>WINDOW PRINT SAVE AS PDF</title>
    </head>
    <body>
        <button class="print">Print</button>
        <script>
            const btn=document.querySelector(".print");
            function printORsave(){
                window.print();
            }
            btn.addEventListener("click",printORsave);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined button ‘Print’ with class attribute ‘print’ for load function ‘printORsave()’ when user clicks on it.
  7. In script variable ‘btn’ defined for refer button ‘Print’ by querySelector() method. This method will collect any html element by attributes like ‘class, id, etc..’ with the help of document object.
  8. Using ‘btn’ we initializing function ‘printORsave()’ call when click event triggered by addEventListener().
  9. In‘printORsave()’ method we generating print() method with the help of window object so it will opens print dialog box there we have to select save as pdf option in designation.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to save as pdf that window print using javascript.

When we executes program on browser we can see button ‘Print’ will display on browser when user clicks on that button.

It will open current window page with built in printing option box at right hand side there user needs to choose save as pdf option near designation label then that page will stored as pdf at our designated folder.

I hope this tutorial on window print save as pdf JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪