Monday 20 June 2011

How To Print HTML From Iframe

 There are many times requirement to print pdf  or HTML content from Iframe. i will show how i had done printing of  iframe directly to printer. here i am calling a html contents into iframe, that was the same html which i used to create a pdf file by HTML2PDF.

<?php $printpdfpath="http://mysite.com/pdfhtml.php";?>
<div>
<iframe name="my_iframe" id="my_iframe"  width="100" height="100" src="<?php echo $printpdfpath;?>" frameborder="0"></iframe>
</div>
first i create an html page at my server and put above code there. which include an Iframe  to show html. now
<input type="button"  id="pdf"  value="Print" onclick="printIframe();">
<script>
function printIframe()
{
    var id="my_iframe";
    var iframe = document.frames ? document.frames[id] : document.getElementById(id);
    var ifWin = iframe.contentWindow || iframe;
    iframe.focus();
    ifWin.printpdfdata(); //a function in iframe html code
    return false;
}

</script>
that function printIframe(); is a cross browser to print content of HTML inside the iframe. and ifWin.printpdfdata(); is a function written in pdfhtml.php to call the print function.
<script>
function printpdfdata(){
     print();
    setTimeout('window.close()',1000);
</script> 

No comments:

Post a Comment