To create a function that opens a print window with the provided innerHTML, you can use the code snippet below:
Code snippet:
openPrintWindow: function (printHtmlStr) {
var me = this;
var dt = new Date().getTime();
var html = '<!DOCTYPE HTML>' +
'<html>' +
'<head>' +
'<meta http-equiv="x-ua-compatible" content="IE=Edge"/>' +
'<link rel="stylesheet" type="text/css" href="appRes/css/print.css?_dc=' + dt + '" media="all" />' +
'</head>' +
'<body>' +
'<div class="print">' + printHtmlStr + '</div>';
html += '</body>';
html += '</html>';
var iframe = this._printIframe;
if (!this._printIframe) {
iframe = this._printIframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.style.display = 'none';
iframe.onload = function () {
setTimeout(function () {
iframe.focus();
iframe.contentWindow.print();
}, 1);
};
}
iframe.src = "about:blank";
var doc = iframe.contentWindow.document;
var windowUrl = window.location;
var uniqueName = new Date();
var windowName = 'Print' + uniqueName.getTime();
this.testwindow = doc.open(windowUrl, windowName);
doc.write(html);
doc.close();
}
Feel free to adapt and use this function in your project for printing purposes.