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();">
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.
<?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{
{
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>//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();
}
}
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.
Hello,
ReplyDeleteThis code is not working on google chrome neigther on Firefox last version.
Google Chrome => loading page
FireFox => Blank page
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.
Deletehttp://stackoverflow.com/questions/6906047/printing-iframe-content-with-jquery
http://stackoverflow.com/questions/9616426/javascript-print-iframe-contents-only
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.
DeleteThank You man.. It worked for me... I was working around for 2 days to do this.. Your code really saved me..
ReplyDeleteThanks again
My Regards...
DeleteWhat 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?
ReplyDeleteThanks a lot !! It saved my time...
ReplyDeleteThis comment has been removed by the author.
ReplyDelete