2

I want to show a url content in new popup window and after it instantly show the print window to print the content of that url, may I ask you that how can I do this?


I edit my question: Really I want to open a popup window, load a specific div of a given url (for print friendly view of a post) and after load finished, open the print window...

have any idea about it?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
sajjad_safari
  • 87
  • 1
  • 2
  • 11
  • http://stackoverflow.com/questions/2603465/using-jquery-to-open-a-popup-window-and-print – jpcanamaque Jul 08 '15 at 00:50
  • Johnroe Thanks for your link, i updated my question,i read your link but not realized what to do about it, please guide me, Thank you so much – sajjad_safari Jul 08 '15 at 01:15
  • is this url that you want to open in new window part of your website? if not, you are better off using your backend to grab that "portion" you want to print, and present in your front end – Ji_in_coding Jul 08 '15 at 01:17
  • If you have thoroughly read the answer on the link that I gave, All you have to do is have your page (the one that needs to be printed) execute `window.print()` on an `onload` event. – jpcanamaque Jul 08 '15 at 01:19
  • yes, in fact in my home page, i want to show a print icon for printing the post page, but as you know it will print existing document not linked post page, because of this i want to open that page first(and not whole the page and only a specisic div to have print frienly view, and after that load finished, print window appears... – sajjad_safari Jul 08 '15 at 01:20
  • Johnroe Thank you for your answering, i'm not an expert in java, may i ask you to write the whole code should i write in my page? that link was explaind not completly at least for me...! Thank you so much – sajjad_safari Jul 08 '15 at 01:26

2 Answers2

5

My solution is quite simple on a current link I already have an inline onclick event definition like: onclick="window.open(....)", I just add ".print()" after ".open()" in order to automatically show printing dialog after loading the URL, I have tested on chrome and firefox for linux and works as I expected.

My code looks something like this:

<a class="btn btn-success" href="#" onclick="window.open('URL_TO_POST','POPUP WINDOW TITLE HERE','width=650,height=800').print()">Print</a>

Hope this is what you are looking for

Carlos Huggins
  • 103
  • 1
  • 6
  • Is there some way to get this to work with a remote url? i.e. When I call this with 'http://www.google.com' as the 'URL_TO_POST' the window opens as expected but print is never triggered. – Amber Nov 02 '17 at 19:22
  • This has got me half way there, thanks! But how can I add a timeout to printing? My content needs a couple of seconds to load before the print dialog appears. – Matt Saunders Oct 24 '18 at 11:05
0

If I understand your question clearly. Here is a simple example:


EDIT 1 : What I did is hide the elements that are not to be printed through CSS media.

1.php

<html>

<script>
    function sample() {
            var x = document.getElementById('txt').value;       
            window.open('2.php?x='+x);
    }
</script>
<input type = "text" id = "txt" />
<button onclick = "sample();"> Sample </button>

</html>

2.php

<html>

<style type="text/css">
   @media print
   {
       #not-print {display:none;}
   }
</style>

<body onload = "window.print()">

 <div id = 'print'>
 <?php

 if(isset($_GET['x'])) {
     echo $_GET['x'];
 } else {
     echo 'Hello World';
 }

 ?>
 </div>

 <div id = 'not-print'>
       This is not printed
 <div>
 </body>

 <html/>

EDIT 2 : See code below:

2.php

<html>

<style type="text/css">
    @media print
    {
        #not-print {display:none;}
    }
</style>


<body>

<div id = 'print'>
    This should be printed!
</div>

<div id = 'not-print'>
This is not printed
<div>

<button onclick = "printdiv()">Print Div </button>

<script>
    function printdiv() {
        var mywindow = window.open("", '_blank');
        mywindow.document.write('<p>' + document.getElementById('print').innerHTML + '</p>');
        mywindow.print();
    }

</script>

</body>

<html/>
jpcanamaque
  • 1,049
  • 1
  • 8
  • 14
  • Thank you johnroe, But my mean was not this, Suppose that we have one page for example 2.php and there is a post in this page,for example with id #post... now i want to open a pop up window, and load the div with post id in the popup window, and after page loading was finished, the print window(for printng the page not echo a string or alert) appear to print the pop up window, and say this i want to put a button in my home page to show a pop up page of my post page... Thank you – sajjad_safari Jul 08 '15 at 01:33
  • Ah. so you want to print a specific `div` on 2.php? – jpcanamaque Jul 08 '15 at 01:35
  • yes, print a div by printer with print window :) (not alerting or echo a message), Thanks – sajjad_safari Jul 08 '15 at 01:39
  • Before anybody answer my question, say that please adapte adapt the code in way that after the popup window opened and loading a specific div content of a url, before that the page loading is not completed, the print window dont appear, Thank you So so much – sajjad_safari Jul 08 '15 at 01:51
  • Is it supposed to have a button that opens print dialog on 2.php? Or have your 2.php open a print dialog `onload`? – jpcanamaque Jul 08 '15 at 01:57
  • no i can't edit 2.php because it affect the whole of my site, i use Wordpress CMs and want to get the post div content in pop up window and after that print it, is there another way that not to be nesseccery edit 2.php? Thanks – sajjad_safari Jul 08 '15 at 02:01