Last active
November 9, 2019 23:09
-
-
Save mjschutz/c920548d6d37ffcc1eb66f7eaff33cf9 to your computer and use it in GitHub Desktop.
Function to print some HTML content on NW.js using a iFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printR(htmlContent) { | |
let doc = nw.Window.get().window.document; | |
let iframe = doc.createElement("iframe"); | |
iframe.style.display = 'none'; | |
doc.body.appendChild(iframe); | |
iframe.onload = () => { | |
console.log('loaded'); | |
if (typeof htmlContent === 'function') { | |
htmlContent(iframe); | |
} else { | |
iframe.contentDocument.body.innerHTML = htmlContent; | |
} | |
iframe.focus(); | |
iframe.contentWindow.print({ autoprint: false }); | |
iframe.contentWindow.onafterprint = function(){ | |
doc.body.removeChild(iframe); | |
} | |
}; | |
iframe.src = 'about:blank'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment