4

Disclaimer: I realize this has been asked a few times. I've been reading up on this for a while. It's how I got as far as I did. But they haven't helped solve my particular problem.

I am trying to load a pdf in a new tab by clicking on a link, and then print the pdf. Currently I have it working in Chrome and Firefox. Internet Explorer is being stubborn.

This is what I have:

<a href="javascript: w=window.open('Senokot_OnlineCoup_50_fr_syrup.pdf'); w.focus(); w.print();" class="smalllink">Print ›</a>

Adding focus didn't really do anything. There will be multiple pdfs on one page to open. Inline javascript isn't the best option, but I'm trying to avoid iframes if possible, and at the moment it's more about getting the code to work than to have the javascript all nice and perfect. I'm not an expert, so getting it to work is my main focus.

I'm not worried about getting the window to close afterwards. I'd tried, and Chrome refused to open the new tab at all, so I was focusing on the printing part.

Any help is appreciated!

Lisa
  • 323
  • 3
  • 14
  • Possible duplicate: https://stackoverflow.com/questions/12276416/how-to-open-a-link-new-tab-with-print-command – Vince Horst May 31 '17 at 23:00
  • @VinceHorst That is actually the very post which got me as far as what I have in the initial question. There were problems in Internet Explorer with that code, hence the post and the note at the beginning of the post acknowledging that it's been asked before. However, if it needs to be closed for that reason, so be it. – Lisa Jun 01 '17 at 22:45

1 Answers1

0

This code worked for me:

function func_name()
{
var printPage = window.open(document.URL, '_blank');
var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
winPrint.document.write('<title>Print  Report</title><br /><br /> Hellow World');
winPrint.document.close();
winPrint.focus();
winPrint.print();
winPrint.close();
}

here my link:

<a href="#" onclick="func_name()">Print_This_Page</a>

Also check that your popups are not blocked. In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers, and the default "new window" behavior. You could do it this way, or by adding an event listener to your DOM object.

Himanshu
  • 59
  • 3
  • 2
    This code appears to print the current page. I do not want to do a "print this page" button. I want to have the link that is clicked on, which would be a PDF, open in a new tab and print. There would be multiple links on the page that would have this function. – Lisa Jul 09 '16 at 09:29