0

I have a button in my web-application (java, jsp, js) that allows printing by taking HTML from selected areas of the screen, and placing it in an iframe called 'iFramePrint' and then printing it.

<iframe name="iFramePrint" frameborder="0" src="Blank.jsp" marginheight="0" marginwidth="0" width="0" height="0"></iframe>

function doPrint() {
    if (window.frames['iFramePrint'].document.body) {

        var str = '<B>'+'<h1>' + document.getElementById('tdMainTitle').innerHTML +'</h1>'+'</B>';
        str += '<BR>';
        etc.
        str=str.replace(/BORDER=0/g ,"border=1");
        window.frames['iFramePrint'].document.body.innerHTML = str; 
        window.frames['iFramePrint'].focus();
        window.frames['iFramePrint'].print(); 
    }
}

This has worked well for a long time, however due to a recent Windows Update, the code no longer works. See below.

https://answers.microsoft.com/en-us/ie/forum/ie11-iewindows_10/cannot-print-single-frames-iframes-popups-after/e431c6e1-5f27-4bef-93ce-d8d9ae23a477

I preferably do not want to wait for Microsoft to fix this - it could be a while. And I do not want to ask clients to uninstall KB4022715 (the Knowledge Base number varies depending on the windows version, but generally speaking it's the security update rollup for Jul, released this month) which does fix the problem.

I have a solution how to solve this - to take the HTML and convert it to a PDF and then allow the user to download the PDF, which is definitely doable. See Converting HTML files to PDF - just wondering if there is a better way.

gordon613
  • 2,770
  • 12
  • 52
  • 81

1 Answers1

0

The best solution I have found so far is to replace the iFramePrint with a new window.

function doPrint() {
        var str = '<B>'+'<h1>' + document.getElementById('tdMainTitle').innerHTML +'</h1>'+'</B>';
        str += '<BR>';
        etc.
        str=str.replace(/BORDER=0/g ,"border=1");
        w=window.open();
        w.document.write('<html><head><title>Printout</title>');
        w.document.write('</head><body >');
        w.document.write(str);
        w.document.write('</body></html>');
        w.document.close();
        w.focus();
        w.print();
        w.close();
}

Thanks to @pratik in window.print() not working in IE and to @jaay in How to print HTML content on click of a button, but not the page? There are other answers there which will probably do the trick without opening a new window.

gordon613
  • 2,770
  • 12
  • 52
  • 81