html: print() embed file

33,561
<script type="text/javascript">
function printPDF(pdfUrl) 
{
    var w = window.open(pdfUrl);
    w.print();
}
</script>

You can call this function printPDF("http://mywebsite.com/myfile.pdf")

Share:
33,561
Rps
Author by

Rps

Make my day!

Updated on December 02, 2020

Comments

  • Rps
    Rps over 3 years

    I am showing a pdf inside a html using:

    <embed id="print" src="file.pdf" type="application/pdf" width="100%" height="100%"> 
    

    I would like to create a button that prints that file.

    I thought it would be something like this; but it isn't working.

    <button onClick="javascript:document.getElementById('print').print();">
    

    Anybody an idea how I can call the print function on an embedded file?

  • Rps
    Rps about 12 years
    Thanx Agercian. the script does open a new window, but the print() isn't doing anything. I am using chrome to do this.
  • agarcian
    agarcian about 12 years
    Is the PDF hosted in the same domain that the page hosting this code is? I believe if you are trying to print from a different server it will give an 'Unsafe Javascript' message.
  • Rps
    Rps about 12 years
    no the pdf is accessed form localserver, stored on my c:/ drive
  • Max Malyk
    Max Malyk over 10 years
    printPDF is a FF addon - addons.mozilla.org/en-US/firefox/addon/printpdf, it does not help to resolve the question for other browsers...
  • Raghavendra N
    Raghavendra N almost 2 years
    Chaining the print method will work - window.open(pdfUrl).print()