How to Print Iframe Content using Jquery?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hi Dev,
Hope all are okey, today i would like to share you how to print iframe content by using javascript. So in this article, i will implement a jquery print iframe content only. If you want to know a good example of print frames contents using jquery then i hope you are in a right place. So you can learn jquery print iframe pdf file.For this you just need to do some step to done javascript print pdf file in iframe.

Here now, i will give you a very simple example how to print iframe content by using jquery and javascript. If you open html code or pdf file on your iframe then you can simply print it by using this jquery.

So now ,you can see bellow solution and bellow full example that will help you.

Solution:

var myIframe = document.getElementById("ipdf").contentWindow;

    myIframe.focus();

    myIframe.print();

    return false;

Read Also: How to Make Textarea Auto Resize using Javascript?

Example:

Make sure that you have mypdf.pdf file on your location, so it will display in your iframe.

So, please keep one file there.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>How to print iframe content using jquery - Codingspoint.com</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
  
<body>
    <button>Print</button>
    <iframe id="ipdf" src="NHPFORMTEST.pdf" width="880" height="900"></iframe>
</body>
   
<script type="text/javascript">
    $("button").click(function(){
        var myIframe = document.getElementById("ipdf").contentWindow;
            myIframe.focus();
            myIframe.print();
            return false;
    });
</script>
</html>

So ,I hope it can help you in your work… Also you can follow us on Facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →

Leave a Reply

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

twenty − 14 =