0

I am trying to print an external source by clicking on a button. I found multiple scripts on the internet but none of them are working.

So my question is, how can I print a specific page by clicking on a button?

Now I have:

<iframe id="frame" src="./test.php"></iframe>

<button onclick="printOtherPage()">Print</button>
<script type="text/javascript">
  function printOtherPage() {
  document.getElementById('frame').contentWindow.window.print();
</script>
John
  • 904
  • 8
  • 22
  • 56
  • What 'scripts' did you try? What errors occur while you're using them? Edit your question, add what you have so far, error logs, etc. – ksno May 31 '16 at 10:54
  • Possible duplicate of [Printing a web page using just url and without opening new window?](http://stackoverflow.com/questions/8240472/printing-a-web-page-using-just-url-and-without-opening-new-window) – Jeroen Bellemans May 31 '16 at 10:58

2 Answers2

0

you can try this

function loadOtherPage() {

    $("<iframe>")                             // create a new iframe element
        .hide()                               // make it invisible
        .attr("src", "/url/to/page/to/print") // point the iframe to the page you want to print
        .appendTo("body");                    // add iframe to the DOM to cause it to load the page
}
TarangP
  • 2,711
  • 5
  • 20
  • 41
İlker A.
  • 11
  • 5
0

To do this:

  1. Create a new iframe element.
  2. Set the src of the iframe to the page you want to print.
  3. In javascript, contentWindow is used to retrieve the document in iframe.

So use this code to print:

document.getElementById('frame').contentWindow.window.print();

Correct me if I am wrong.