13

In the following link

<a href=\"#\" onclick=javascript:print(\"\") style=\"color:blue;\">Print</a>"
<script>
 function print()
 {
      //How to print the contents of another page
 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hulk
  • 32,860
  • 62
  • 144
  • 215

6 Answers6

30

I know it´s an old question, but you can do it like this:

function printExternal(url) {
    var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
    printWindow.addEventListener('load', function(){
        printWindow.print();
        printWindow.close();
    }, true);
}

Tested in Firefox and Chrome. IE9 doesn´t work.


Edit 2020-04-03

No longer work on Chrome, code adapted from Coder answer here:

function printExternal(url) {
    var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');

    printWindow.addEventListener('load', function() {
        if (Boolean(printWindow.chrome)) {
            printWindow.print();
            setTimeout(function(){
                printWindow.close();
            }, 500);
        } else {
            printWindow.print();
            printWindow.close();
        }
    }, true);
}
Alejandro Fiore
  • 1,117
  • 14
  • 18
22

If you already have an external page( letterprint.php ), put that page in a hidden iframe and print the content of iframe by using onclick attribute in a button.

<iframe src="letterprint.php" style="display:none;" name="frame"></iframe>

<input type="button" onclick="frames['frame'].print()" value="printletter">
151291
  • 3,308
  • 7
  • 48
  • 81
  • 3
    This worked for me too but I had to do some trick in the iframe because if I set display:none it does not print. So I set the iframe style like style="visibility:hidden; height:1px; width:1px". – rolo1091 Apr 09 '18 at 15:32
  • 3
    This worked fantastic in current browsers, most eloquent way to do this without creating a pop-up (Which was the method I used to use) - needs more upvotes. – Tarquin Jan 15 '19 at 22:20
15

An alternative is to link to the page with a get variable and then call the print function.

For your link -

<a href="print-page.php?print=1">Print other page</a>

Then on your print page (or all pages)

<script type="text/javascript">
<? if(isset($_GET['print'])) { ?>
window.print();
<? } ?>
</script>
sebtucknott
  • 259
  • 3
  • 4
  • Oh, right. That's PHP. Took me a sec. If you wanted to stay in plain javascript you could do something like this: `if (/\bprint=1\b/.test(window.location.search)) window.print();` – Arel Nov 23 '18 at 16:21
1

You can print external page by creating window object of another page and calling print method of that page.
URL can be passed in argument or can be hardcoded in function depends on use case

function print(url) {
    var printWindow = window.open( url);
    printWindow.print();
};
Rupesh Agrawal
  • 645
  • 8
  • 22
-1

Think about the security/embarrassment issues that would exist if this was possible. Thankfully, browsers won't allow you to do that.

The closest you can get is fetching the page via AJAX, replacing the current DOM with the new page, and printing with JS's normal print() method.

Amy B
  • 17,874
  • 12
  • 64
  • 83
  • not an easy way you are talking about for a newbie at least, what is the security issue by the way? –  Apr 05 '10 at 11:48
  • @phpBOY - The issue is immature website owners printing porno websites when you don't expect it. – Amy B Apr 05 '10 at 12:07
-12

I think you can print the contents of the current page not external page.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • We can also print the contents of other page using URL with iframe. Check [this stackoverflow link](https://stackoverflow.com/a/13724670/5485645) – Shivakrishna Mar 27 '18 at 13:48
  • We can print content of other page please check stackoverflow link –(https://stackoverflow.com/a/51306322/4225796) – Rupesh Agrawal Jul 12 '18 at 15:13