2

I have a printUrl javascript/jquery function that loads a print friendly version of my web page into an iFrame and prints it. It seems to work in Chrome, Firefox, and IE but I can't get it to work in Microsoft Edge browser. The print dialog comes up but with the message "Nothing sent to print" in red. Any help would be appreciated. Function below:

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);
});
}
Patrick
  • 13,872
  • 5
  • 35
  • 53
Jimmy Hoffa
  • 29
  • 1
  • 4

2 Answers2

1

This is an issue, and already posted in MS Connect website [Edge] Printing iframe window ends up printing top window

Moe
  • 197
  • 6
0

We found this solution:

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

posted here on StackOverflow

Community
  • 1
  • 1
prespic
  • 1,635
  • 1
  • 17
  • 20