Charles Smith II
Charles Smith II

Reputation: 21

Want to open new window and then display print dialog for/in that window

I have the following function being called onclick by an anchor. It opens the new window like I want but, the issue is that the print dialog box won't display unless the 'URL' field of the open() function is empty.

I need to be able to open a specific URL ('https://www.example.com/file.pdf'), so can't leave it blank.

<script>
  var pdfw;
  function printPdf() {
    // pull url
    url = document.getElementById('print').getAttribute('href');
    console.log("pull");

    // open window
    pdfw = window.open('', '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');
    console.log("open");

    // print window
    pdfw.focus().print();
    console.log("print");
  }
</script>

Brings up new window, which is blank because the URL isn't specificed of course, and print dialog box:

pdfw = window.open('', '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');

Brings up new window displaying desired file, but does not display the print dialog box:

pdfw = window.open(url, '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');

I would say it is some kind of race condition issue, but nothing else is using the print function on any of the pages the script runs on, so I'm pretty sure that's not it.

Any help or advice with this issue would be greatly appreciated!

Upvotes: 2

Views: 6911

Answers (2)

Muhammad
Muhammad

Reputation: 7344

You can do it the following way.

var win = window.open("", "Print Page", "height=600,width=800");
win.document.write("<h1>Welcome to Print Page</h1>");
win.print();

JsFiddle Here

Upvotes: 1

Ghulam Mohayudin
Ghulam Mohayudin

Reputation: 1113

After opening the window you can call

 pdfw = window.open(url, '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');
 pdfw.focus();
// you need to call print after window loads like this
 pdfw.onload=function(){
                pdfw.print();
              }

for more details you can see this

Upvotes: 1

Related Questions