0

I have one print button & on click of that button I want to print other pdf as per the link will provide not the same page. but for now print functionality is working on page load not on button click

function print (doc) {
var objFra = document.createElement('iframe');
objFra.style.visibility = 'hidden';
objFra.src = doc;
document.body.appendChild(objFra);
objFra.contentWindow.focus();
objFra.contentWindow.print();
}
print();

<input type="button" id="bt" onClick="print()" value="Print PDF"/> 
DritiP
  • 11
  • 4

2 Answers2

1

Your print method is getting invoked on button click, but since it's visibility is set to hidden, it is not visible. You also have print method invoked on the page load.

function print (doc) {
    var objFra = document.createElement('iframe');
    //objFra.style.visibility = 'hidden';
    objFra.src = doc;
    document.body.appendChild(objFra);
    objFra.contentWindow.focus();
    objFra.contentWindow.print();
}
print('path');
<input type="button" id="bt" onClick="print('somePath')" value="Print PDF"/> 
random
  • 7,756
  • 3
  • 19
  • 25
0

You may call your print function on $(document).ready(function(){}) function.

meDeepakJain
  • 156
  • 1
  • 13