Friday 17 June 2011

How To Print PDF From Iframe

There are many times requirement to print pdf from Iframe. or PDF embedded in HTML. i will show how i had done printing of PDF from iframe directly to printer.
<?php $printpdfpath="http://mysite.com/upload/sample1.pdf";?>

<div>
<iframe name="my_iframe" id="my_iframe"  width="100" height="100" src="<?php echo $printpdfpath;?>" frameborder="0"></iframe>

<embed
    type="application/pdf"
    src="<?php echo $printpdfpath;?>"
    id="pdfDocument"
    width="100"
    height="100">
</embed>
</div>
first i create an html page at my server and put above code there. which include an Iframe and <embed> to show pdf at html. now
<input type="button"  id="pdf"  value="Print" onclick="printIframe();">

<script>
function printIframe()
{
    if (navigator.userAgent.indexOf("Firefox")!=-1){
        var iframe_window = window.frames["my_iframe"];       
        frame_window.print();
    }
    else if(navigator.userAgent.indexOf("MSIE")!=-1){
        var x = document.getElementById("pdfDocument");
        x.print();       
    }
    else if(navigator.userAgent.indexOf("Chrome")!=-1){
        document.my_iframe.focus();
        document.my_iframe.print();   
    }
    else{   
        // Safari browser or other one   
        //document.getElementById('my_iframe').focus();
        //document.getElementById('my_iframe').onload = setTimeout('iframe.print()',2500);
        //var ifWin = document.getElementById('my_iframe').contentWindow;
        //ifWin.focus();
        //ifWin.print();
        //printpdf();
       
    }   

}
</script>
at above javascript i had put a check for different browser, there is problem with safari to print pdf via Iframe. many time developers face this while using above function which i had commented at Safari section. they print blank pages. so i had use printpdf(); a user define function to call pop window to print.

8 comments:

  1. Hello,

    This code is not working on google chrome neigther on Firefox last version.
    Google Chrome => loading page
    FireFox => Blank page

    ReplyDelete
    Replies
    1. Please check first, weather pdf is loaded into iframe by increasing the width and height of frame to 300px,so that you can view the content of pdf on HTML page. then click print button. And if still print doesn't work, please refer to below tutorial links.

      http://stackoverflow.com/questions/6906047/printing-iframe-content-with-jquery

      http://stackoverflow.com/questions/9616426/javascript-print-iframe-contents-only

      Delete
    2. Must use Adobe SDK javascript API. Once the web browser shows the pdf, Adobe plug-in took over, the javascipt for the HTML DOM won't work.

      Delete
  2. Thank You man.. It worked for me... I was working around for 2 days to do this.. Your code really saved me..
    Thanks again

    ReplyDelete
  3. What if I want the PDF to print automatically without having to click a print button? How do I tell if it has been rendered and is ready to print?

    ReplyDelete
  4. Thanks a lot !! It saved my time...

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete