3

I have a pdf url, embedded in a rectangular area of an iframe. I am trying to print it on click of a button . My code looks like this :

JavaScript to print :

function printPDF(pdfUrl)
{
var w = window.open(pdfUrl);
w.print();
w.close();
}

HTML-Code :

<table>
<tr>
<td><input type="submit"  value="Print"
name="Submit" id="printbtn"
onclick="printPDF('http://www.irs.gov/pub/irs-pdf/fw4.pdf')" /></td>
</tr>

Now in MF it is coming like this :

enter image description here

The problem , its not working properly in chrome and IE. I have chrome 20 and IE 8. In chrome the print window gets loaded in a new tab(which is ok) but pdf does not get loaded in preview. In IE the pdf just gets opened in a new tab and print prompt does not come up.

Before any one asks me, pdf might have inherent print functionality. but the pdfs that i am processing need to have a print button. People, kindly help me out here. Is there a solution, which is applicable in all the 3 browsers .

PointedEars
  • 14,752
  • 4
  • 34
  • 33
The Dark Knight
  • 5,455
  • 11
  • 54
  • 95

1 Answers1

2

Folks, just got a solution , which works fine for me .

function printPage(htmlPage)
{
    var w = window.open("about:blank");
    w.document.write(htmlPage);
    if (navigator.appName == 'Microsoft Internet Explorer') window.print();
    else w.print();
}

So, if it is MSIE, then we can just use : window.print();

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
  • I got this one from : http://stackoverflow.com/questions/2555697/window-print-not-working-in-ie by Mr Kyle – The Dark Knight Dec 19 '12 at 09:07
  • What's the motivation behind using `window.print` for IE and `w.print` for everything else? Surely that just has the effect that if you're in internet explorer, you end up printing the page from which you called `printPage` instead of the URL that you passed to it? – Mark Amery Jan 10 '20 at 13:08