2015-08-24 113 views
2

我有一个printUrl javascript/jquery函数,它将我的网页的打印友好版本加载到iFrame中并打印出来。它似乎可以在Chrome,Firefox和IE中使用,但我无法在Microsoft Edge浏览器中使用它。打印对话框出现,但显示红色的消息“无法打印”。任何帮助,将不胜感激。以下功能:Microsoft Edge浏览器和打印

function printUrl(url) { 
 
$('body').append('<iframe width="1" height="1" id="printFrame" style="display: none; @media print { display: block; }"/>'); 
 
$('#printFrame').attr('src', url); 
 
$('#printFrame').load(function() { 
 
    var ua = window.navigator.userAgent; 
 
    var msie = ua.indexOf("Trident"); // detect if IE 
 

 
    if (msie > 0) { 
 

 
     var target = document.getElementById('printFrame'); 
 
     try { 
 
      target.contentWindow.document.execCommand('print', false, null); 
 
     } catch (e) { 
 
      target.contentWindow.print(); 
 
     } 
 
    } else { 
 
     // this code executes for Edge printing as well as Chrome, Firefox 
 
     var frame = document.getElementById('printFrame'); 
 
     if (!frame) { 
 
      $.alert("Error: Can't find printing frame."); 
 
      return; 
 
     } 
 
     frame = frame.contentWindow; 
 
     frame.focus(); 
 
     frame.print(); 
 
    } 
 
    setTimeout(function() { 
 
     $('#printFrame').remove() 
 
    }, 500); 
 
}); 
 
}

回答

0

我们发现这个解决方案:

parent.document.getElementsByName("pdfjs-frame")[0].contentWindow.document.execCommand("print", false, null); 

posted here on StackOverflow