How do I print a pdf from within an iframe?

2019-06-01 05:01发布

问题:

At cbjsonline.com, I'm trying to have a pdf in an iframe print automatically with javascript.

Currently, my code is - (connected to the onclick of the link that opens the iframe) - document.getElementById('fancy_frame').onload = setTimeout('window.print()',2500);

Any suggestions? This method only works in safari.

回答1:

Try passing a function pointer to setTimeout, instead of an expression that gets eval'd.

document.getElementById('fancy_frame').onload = setTimeout( printWindow, 2500 );

// implemented in the HTML that is loaded in 'fancy_frame'
function printWindow()
{
    window.print();
}


回答2:

OK, I decided to let the user print the page. I think iFrames with PDFs are too variable trying to print with javascript. If anyone wants to try this, this is what I'd recommend. Use the jquery load, which checks for assets, not just loading, not the onload handeler, because the delay for adobe reader varies greatly across computers. Also, try naming and focusing the iframe before printing (by name, so iframe.print(), instead of window.print()), that way it won't try to print the page your currently on. It might be a better idea just to use the scribd ipaper viewer for this application.