Printing contents of another page

39,681

Solution 1

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

Solution 2

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);
}

Solution 3

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">

Solution 4

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>

Solution 5

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();
};
Share:
39,681
Hulk
Author by

Hulk

Updated on April 04, 2020

Comments

  • Hulk
    Hulk about 4 years

    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
     }
    
  • Admin
    Admin about 14 years
    not an easy way you are talking about for a newbie at least, what is the security issue by the way?
  • Amy B
    Amy B about 14 years
    @phpBOY - The issue is immature website owners printing porno websites when you don't expect it.
  • Aldry Wijaya
    Aldry Wijaya almost 11 years
    I would say many thanks to you. This trick works for me. It really helps for kiosk mode at chrome..
  • iamsmug
    iamsmug about 10 years
    This method works but redirects my main page to the index page.
  • ndsmyter
    ndsmyter about 10 years
    Although your answer looks like a good possible solution, it is better not to reply on threads that haven't been active in a couple of years.
  • sebtucknott
    sebtucknott about 9 years
    Why not? I was looking for a solution, found this thread. I worked out my own solution and hope that is helps someone else...
  • TheRealJAG
    TheRealJAG over 7 years
    In Chrome 55.0.2883.59 it spawns the window, doesn't print then closes.
  • Jay
    Jay almost 7 years
    Yeah it's 2017 now and Google led me here. This was the exact answer I sought.
  • Shivakrishna
    Shivakrishna over 6 years
    We can also print the contents of other page using URL with iframe. Check this stackoverflow link
  • rolo1091
    rolo1091 about 6 years
    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".
  • Rupesh Agrawal
    Rupesh Agrawal almost 6 years
    We can print content of other page please check stackoverflow link –(stackoverflow.com/a/51306322/4225796)
  • Arel
    Arel over 5 years
    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();
  • Tarquin
    Tarquin over 5 years
    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.