I want to trigger the print of a PDF file which I load in and iframe.
After looking around, I came up with the following bit
Try the following, might work in all browsers. ( I have tested with IE8 and chrome only )
<style type="text/css">
@media print
{
.dontprint{display:none}
}
</style>
<script type="text/javascript">
function printIframePdf(){
window.frames["printf"].focus();
try {
window.frames["printf"].print();
}
catch(e){
window.print();
console.log(e);
}
}
function printObjectPdf() {
try{
document.getElementById('idPdf').Print();
}
catch(e){
printIframePdf();
console.log(e);
}
}
function idPdf_onreadystatechange() {
if (idPdf.readyState === 4)
setTimeout(printObjectPdf, 1000);
}
</script>
<div class="dontprint" >
<form><input type="button" onClick="printObjectPdf()" class="btn" value="Print"/></form>
</div>
<iframe id="printf" name="printf" src="http://pdfUrl.pdf" frameborder="0" width="440" height="580" style="width: 440px; height: 580px;display: none;"></iframe>
<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"
width="440" height="580" style="width: 440px; height: 580px;" type="application/pdf"
data="http://pdfUrl.pdf">
<embed src="http://pdfUrl.pdf" width="440" height="580" style="width: 440px; height: 580px;" type="application/pdf">
</embed>
<span>PDF plugin is not available.</span>
</object>