How do I print a PDF file using javascript in Opera?
I am able to use javascript to print PDF file in Internet Explorer and Firefox, as such:
<开发者_如何学Pythonscript type="text/javascript">
<!--
if (navigator.appName == 'Microsoft Internet Explorer')
{
document.write('<object id="agreementPDF" type="application/pdf" data="file.pdf" width="100%" height="500"></object>');
}
else
{
document.write('<iframe id="agreementFrame" src="file.pdf" width="100%" height="500px"></iframe>');
}
//-->
</script>
<script type="text/javascript">
<!--
function printAgreement()
{
if (navigator.appName == 'Microsoft Internet Explorer')
{
document.getElementById('agreementPDF').print();
}
else
{
var agreement = document.getElementById('agreementFrame');
agreement.focus();
agreement.contentWindow.print();
}
}
//-->
</script>
<input type="button" name="print" onclick="printAgreement();" value="Print" />
However, in Opera, the print function does not work.
How do I make it work?
Thanks.
Looks like Opera needs a little more hand holding when it comes to the print dialog. Here are some very similar questions previously asked/answered on stackoverflow:
window.print not working with opera browser
print() not working on opera browser
Print iframe content in Opera and Chrome
Seems like you either need to use a different *.print() function or trigger the event after the page has finished loading.
Hope these links help.
Have you tried printing on the window's onload event?
window.onload = function(){
window.print();
};
精彩评论