I usually do something similar to the approach given by How to Use JavaScript to Print a PDF (eHow.com), using an iframe
.
function printTrigger(elementId) {
var getMyFrame = document.getElementById(elementId);
getMyFrame.focus();
getMyFrame.contentWindow.print();
}
(an onClick
on an a
or button
or input
or whatever you wish)
<input type="button" value="Print" onclick="printTrigger('iFramePdf');" />
<iframe id="iFramePdf" src="myPdfUrl.pdf" style="display:none;"></iframe>
Bonus Idea #1 - Create the iframe
and add it to your page within the printTrigger();
so that the PDF isn't loaded until the user clicks your "Print" button, then the javascript can attack! the iframe and trigger the print dialog.
Bonus Idea #2 - Extra credit if you disable your "Print" button and give the user a little loading spinner or something after they click it, so that they know something's in process instead of clicking it repeatedly!