Js calls the browser print module to achieve the click button to trigger the custom function

  • 2020-03-30 02:24:34
  • OfStack

Main contents of this paper:

If you use the print button that comes with the browser, you cannot do the rest of the printing-related operations, such as passing the print count, and so on. So you need to save the ability to print in the browser and add it to the button you add, so you can click the button and trigger a custom function at the same time.
 
<input type="button" class="btnPrint" id="btnPrint" value=" print " /> 
<iframe frameborder=0 width="100%" height="400px" id="printPDFIframe" src=""<span style="white-space:pre"> </span>></iframe> 

 
<script type="text/javascript"> 
$("#btnPrint").click(function(){ 
<pre code_snippet_id="248441" snippet_file_name="blog_20140321_2_4849863" name="code" class="javascript"><span style="white-space:pre"> </span>printPDF();</pre> }); function printPDF(){ if (navigator.appName == 'Microsoft Internet Explorer'){ //Wait until PDF is ready to print if(typeof document.getElementById("printPDFIframe").print == 'undefined'){ setTimeout(function(){printPDF("printPDFIframe");}, 
1000); }else{ var x = document.getElementById("printPDFIframe"); x.print(); } }else{ PDFIframeLoad(); // for chrome } } //for Chrome  Have to cancel / confirm ,  Unable to send directly to printer  function PDFIframeLoad() { debugger; var iframe = document.getElementById("printPDFIframe"); if(iframe.src) 
{ var frm = iframe.contentWindow; frm.focus();// focus on contentWindow is needed on some ie versions frm.print(); return false; } }</script> 
<pre></pre> 
<pre></pre> 

Related articles: