Printing contents of another page
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();
};
Hulk
Updated on April 04, 2020Comments
-
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 about 14 yearsnot an easy way you are talking about for a newbie at least, what is the security issue by the way?
-
Amy B about 14 years@phpBOY - The issue is immature website owners printing porno websites when you don't expect it.
-
Aldry Wijaya almost 11 yearsI would say many thanks to you. This trick works for me. It really helps for kiosk mode at chrome..
-
iamsmug about 10 yearsThis method works but redirects my main page to the index page.
-
ndsmyter about 10 yearsAlthough 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 about 9 yearsWhy not? I was looking for a solution, found this thread. I worked out my own solution and hope that is helps someone else...
-
TheRealJAG over 7 yearsIn Chrome 55.0.2883.59 it spawns the window, doesn't print then closes.
-
Jay almost 7 yearsYeah it's 2017 now and Google led me here. This was the exact answer I sought.
-
Shivakrishna over 6 yearsWe can also print the contents of other page using URL with iframe. Check this stackoverflow link
-
rolo1091 about 6 yearsThis 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 almost 6 yearsWe can print content of other page please check stackoverflow link –(stackoverflow.com/a/51306322/4225796)
-
Arel over 5 yearsOh, 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 over 5 yearsThis 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.